2 回答

TA贡献1790条经验 获得超9个赞
尝试pandas:
import pandas as pd
df = pd.read_csv('your_file.csv', header=None)
(df.ffill() # fill the blank with the previous Name
.groupby([0])[1] # collect those with same name
.apply(list) # put those in a list
.to_dict() # make a dictionary
)
输出:
{'Name1': ['Value1', 'Value2', 'Value3'],
'Name2': ['Value40', 'Value50', 'Value60'],
'Name3': ['Value5', 'Value10', 'Value15']}
更新:纯 python(3) 解决方案:
with open('your_file.csv') as f:
lines = f.readlines()
d = {}
for line in lines:
row = line.split(',')
if row[0] != '':
key = row[0]
d[key] = []
d[key].append(row[1])
d

TA贡献1810条经验 获得超5个赞
我认为您面临的问题是由于您的嵌套循环。两个循环都指向同一个迭代器。您将在找到 Name1 后开始第二个循环,并在找到 Name2 时将其中断。到外部循环在中断后继续时,您已经跳过了 Name2。
您可以在同一个循环中同时拥有这两个条件:
# with open("GroupsCSV.csv") as csv_file:
# reader = csv.reader(csv_file)
reader = [[1,2,3],[None,5,6]] # Mocking the csv input
objlist = []
for row in reader:
if row[0] and row[2]:
objlist.clear()
objlist.append(row[2])
elif not row[0] and row[2]:
objlist.append(row[2])
print(objlist)
编辑:我更新了代码以提供可测试的输出。打印输出如下所示:
[3]
[3, 6]
添加回答
举报