一. txt文件的读取与保存
def text_save(filename, data):#filename为写入txt文件的路径,data为要写入数据列表.
file = open(filename,'a')
for i in range(len(data)):
s = str(data[i]).replace('[','').replace(']','')#去除[],这两行按数据不同,可以选择
s = s.replace("'",'').replace(',','') +'\n' #去除单引号,逗号,每行末尾追加换行符
file.write(s)
file.close()
print("保存txt文件成功")
# 读取txt文件
with open("Dpreci.txt") as f:
line = f.readlines()
Dpreci = []
for line in line:
Dpreci.append(float(line))
print('读取txt成功')
二. csv文件的读取与保存
def data_write_csv(file_name, datas):#file_name为写入CSV文件的路径,datas为要写入数据列表
file_csv = codecs.open(file_name,'w+','utf-8')
writer = csv.writer(file_csv, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
for data in datas:
writer.writerow(str(data))
print("保存csv文件成功")
三. json文件的读取
path_json = path + name + '.json'
with open(path_json, 'r') as load_f:
load_dict = json.load(load_f)