4 回答
TA贡献1911条经验 获得超7个赞
问题是 CSV 格式不正确,列表周围用双引号引起来
list
是 python 数据类型,所以它不应该用作变量名。lists
通过将列用双引号 引起来修复 CSV 文件
使用此解决方案
pandas
ast.literal_eval
将字符串评估回列表
import pandas as pd
from ast import literal_eval
# fix the csv file by wrapping the list with quotes
with 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 lists
df = pd.read_csv('test.csv', converters={'lists': literal_eval})
# display(df)
id name lists
0 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]:
list
with open
# converts
id,name,lists
1,Beans,[1,2,3]
2,Spam,[5,6,7]
5,Spam,[7,8,9]
6,Steak,[]
# into
id,name,lists
1,Beans,"[1,2,3]"
2,Spam,"[5,6,7]"
5,Spam,"[7,8,9]"
6,Steak,"[]"
TA贡献1848条经验 获得超6个赞
如果您无法更改输入文件,您仍然可以使用以下代码处理列表列 -
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)
TA贡献1946条经验 获得超3个赞
您可以像这样引用您的 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 csv
with 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]]
TA贡献1841条经验 获得超3个赞
问题与您的 csv 文件有关。如果那是实际文件的样子,那么它将按照您说的那样进行。将列表用双引号括起来,如下所示:
id,name,list
1,Beans,"[1,2,3]"
2,Spam,"[5,6,7]"
5,Spam,"[7,8,9]"
添加回答
举报