use*_*589 1 python csv parsing dictionary
['*a*', '*b*', '*c*', '*d*', '*f*','*g*']
['11', '22', '33', '44', '', '55']
['66', '77', '88', '', '99', '10']
['23', '24', 'sac', 'cfg', 'dfg', '']
Run Code Online (Sandbox Code Playgroud)
需要在字典中输入:
{a : ('11','66','23'),b : ('22','77','24'),c : ('33','88','sac'),d :('44','','cfg')}
Run Code Online (Sandbox Code Playgroud)
从CSV文件中读取行:
import csv
csvFile = csv.reader(open("sach.csv", "rb"))
for row in csvFile:
print row
Run Code Online (Sandbox Code Playgroud)
上面显示的代码,上面显示的行的输出有很多列表.请帮我把它换成字典格式,如上图所示.
压缩行:
with open("sach.csv", "rb") as csv_infile:
reader = csv.reader(csv_infile)
yourdict = {r[0].replace('*', ''): r[1:] for r in zip(*reader)}
Run Code Online (Sandbox Code Playgroud)
该zip()函数为您配对,通过reader使用*参数传递对象,Python将循环遍历CSV行并将每行作为单独的参数传递给zip().