一、从文件中读取数据
open()中需要传入路径和编码方式
file_path = '../Tkinter/dbBase.py'
with open(file_path,encoding='utf-8') as file_object:
content = file_object.read()
print(content)
逐行读取
file_path = '../Tkinter/dbBase.py'
with open(file_path,encoding='utf-8') as file_object:
for line in file_object:
print(line.rstrip())
创建一个包含文件各行内容的列表
file_path = '../Tkinter/dbBase.py'
with open(file_path,encoding='utf-8') as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
二、写入文件
1.写入空文件
file_path = 'D:\Develop software\PyCharm\data.txt'
with open(file_path,'a') as file_object:
file_object.write("i am handsome")
三、异常
四、存储数据
使用json.dump()和json.load()
import json
numbers = [2,3,5,7,11,13]
filename = './number.json'
with open(filename,'w') as f:
json.dump(numbe
# 读取json文件
import json
filename = 'number.json'
with open(filename) as f:
numbers = json.load(f)
print(numbers)
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。