-
Smart猫小萌
问题是 CSV 格式不正确,列表周围用双引号引起来list是 python 数据类型,所以它不应该用作变量名。lists通过将列用双引号 引起来修复 CSV 文件使用此解决方案pandasast.literal_eval将字符串评估回列表import pandas as pdfrom ast import literal_eval# fix the csv file by wrapping the list with quoteswith open('test.csv', 'r+', newline='') as f: rows = [s.replace(',[', ',"[').replace(']', ']"').strip() for s in f.readlines()] f.seek(0) f.truncate() f.writelines(s + '\n' for s in rows)# read the csv and evaluate the list column as listsdf = pd.read_csv('test.csv', converters={'lists': literal_eval})# display(df) id name lists0 1 Beans [1, 2, 3]1 2 Spam [5, 6, 7]2 5 Spam [7, 8, 9]3 6 Steak []print(type(df.loc[0, 'lists']))[out]:listwith open# convertsid,name,lists1,Beans,[1,2,3]2,Spam,[5,6,7]5,Spam,[7,8,9]6,Steak,[]# intoid,name,lists1,Beans,"[1,2,3]"2,Spam,"[5,6,7]"5,Spam,"[7,8,9]"6,Steak,"[]"
-
慕勒3428872
如果您无法更改输入文件,您仍然可以使用以下代码处理列表列 -with open('test.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) stringVal = "," for row in reader: newList = row['list'] + "," + stringVal.join(row[None]) print(newList)
-
智慧大石
您可以像这样引用您的 csv 文件:"id","name","list""1","Beans","[1,2,3]""2","Spam","[5,6,7]""5","Spam","[7,8,9]"然后您可以使用csv带有可选参数的模块来读取它quoting=csv.QUOTE_ALL。在此之后,您可以将这些“字符串列表”转换为常规列表。例如,如果这些“字符串列表”只是数字,那么您可以过滤结果,然后将其转换为整数。这是代码:import csvwith open('test.csv', 'r') as fh: data = list(csv.reader(fh, quoting=csv.QUOTE_ALL)) for i in range(1, 4): data[i][2] = list(filter(lambda c: str.isnumeric(c), data[i][2])) data[i][2] = list(map(lambda c: int(c), data[i][2])) print(*data, sep='\n')结果:['id', 'name', 'list']['1', 'Beans', [1, 2, 3]]['2', 'Spam', [5, 6, 7]]['5', 'Spam', [7, 8, 9]]
-
偶然的你
问题与您的 csv 文件有关。如果那是实际文件的样子,那么它将按照您说的那样进行。将列表用双引号括起来,如下所示: id,name,list 1,Beans,"[1,2,3]" 2,Spam,"[5,6,7]" 5,Spam,"[7,8,9]"