推荐使用第3,4种方法打开保存.csv
1.使用csv打开
import csv
import os
with open(os.path.join(Path1,Path2), 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
2.用python的自身语法打开
import os
with open(os.path.join(Path1,Path2), 'r') as f:
for line in f:
items = line.strip().split(',')
print(items )
3.使用pandas打开pd.read_csv
import numpy as np
import pandas as pd
from pandas import DataFrame
filepath=os.path.join(Path1,Path2)
df=pd.read_csv(filepath,header=0,encoding="gbk")
print(df)
关于具体使用方法:https://www.cnblogs.com/wyy1480/p/10322336.html
3.使用pandas保存
import pandas as pd
list=[[1,2,3],[4,5,6],[7,9,9]]
# 下面这行代码运行报错
# list.to_csv('e:/testcsv.csv',encoding='utf-8')
name=['one','two','three']
test=pd.DataFrame(columns=name,data=list)#数据有三列,列名分别为one,two,three
print(test)
test.to_csv('e:/testcsv.csv',encoding='gbk')
4.保存为npy文件
np.save(file_to_save, list)