import pandas as pd #写入 list = [[1, 2, 3], [4, 5, 6], [7, 9, 9]] name = ['one', 'two', 'three'] test = pd.DataFrame(columns=name, data=list) # 数据有三列,列名分别为one,two,three test.to_csv('test.csv') #读 data = pd.read_csv('test.csv') data = data.loc[:,:].values.tolist() for i in range(len(data)): print(data[i][1:])
- 将上面代码封装后
def get_dataList_after_read_csvFile(readFileName, usecols=''): data = pd.read_csv(readFileName,usecols=usecols).loc[:, :].values.tolist() return data def list_to_writeCsv(name,result_list,writeFileName): test = pd.DataFrame(columns=name, data=result_list) # test.to_csv(writeFileName)
- 将字符串类型的列表转化成LIST
import ast str_list = "[11.23,23.34]" list_list = ast.literal_eval(str_list) print(type(list_list))
def read_csv(read_file): data = pd.read_csv(read_file).loc[:, :].values.tolist() return data def write_csv(write_file,res): with open(write_file, 'w', newline='', encoding='utf-8') as f: w = csv.writer(f) for i in tqdm(range(len(res))): w.writerow(res[i])