[Python] Open/view pkl csv json txt file

View/write txt file

# 查看
with open('./task.txt','r') as f:
    lst = f.read()
    lst = eval(lst)

# 写入
with open("./test.txt","w") as f:
    f.write(lst)

View/write pkl file

import pickle

# 查看
f = open('your_file_name.pkl','rb')
info = pickle.load(f)
print(info)

# 保存方法1
with open('./data.pkl', 'wb') as f:
	pickle.dump(data,f)

# 保存方法2
total_file = "./data_list.pkl"
total_file = open(total_file, 'wb')
pickle.dump(data_list, total_file)

This pkl file is a file generated by the pickle library; it
is not a pkl file of the neural network model generated by the torch framework;

View/write csv file

import csv

# 查看
csv_path = './data.csv'
with open(csv_path, 'r') as f:
    reader = csv.reader(f)
    total_lst = []
    for row in reader:
        mini = []
        for i in row:
            mini.append(copy.deepcopy(float(eval(i))))
        # print(row)
        total_lst.append(copy.deepcopy(mini))
    # print(total_lst)

# 保存
data = []
with open("./data.csv",'w',newline='') as f:
    writer = csv.writer(f)
    writer.writerows(data)

Guess you like

Origin blog.csdn.net/ao1886/article/details/109106273