Python 读取csv文件:列表索引超出范围
我应该读一个关于唐纳德·特朗普的facebook更新的CSV文件。我需要在如下列表中创建字典:Python 读取csv文件:列表索引超出范围,python,python-3.x,Python,Python 3.x,我应该读一个关于唐纳德·特朗普的facebook更新的CSV文件。我需要在如下列表中创建字典: [{'link_name': 'Timeline Photos', 'num_angrys': '7', 'num_comments': '543', 'num_hahas': '17', 'num_likes': '6178', 'num_loves': '572', 'num_reactions': '6813', 'num_sads': '0', 'num_shares': '359', 'nu
[{'link_name': 'Timeline Photos',
'num_angrys': '7',
'num_comments': '543',
'num_hahas': '17',
'num_likes': '6178',
'num_loves': '572',
'num_reactions': '6813',
'num_sads': '0',
'num_shares': '359',
'num_wows': '39',
'status_id': '153080620724_10157915294545725',
'status_link': 'https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10157915294545725/?type=3',
'status_message': 'Beautiful evening in Wisconsin- THANK YOU for your incredible support tonight! Everyone get out on November 8th - and VOTE! LETS MAKE AMERICA GREAT AGAIN! -DJT',
'status_published': '10/17/2016 20:56:51',
'status_type': 'photo'},
使用代码。我需要获得前两个状态更新,但当我输入代码时,会出现一个错误,上面写着“列表索引超出范围”
这是密码
def read_csv(input_file, delimiter=","):
# your code here
import csv
csv_data= []
with open(filename, "r") as csvfile:
for row in csvfile:
row = row.strip("\n")
columns = row.split(",")
dict_row = {"link_name": columns [0],
"num_angrys": columns [1],
"num_comments":columns[2],
"num_hahas": columns [3],
"num_loves": columns [4],
"num_reactions": columns [5],
"num_sads": columns [6],
"num_shares": columns[7],
"num_wows": columns [8],
"status_id": columns[9],
"status_link": columns[10],
"status_message": columns [11],
"status_published": columns[12],
"status_type": columns[13]}
csv_data.append(dict_row)
filename = "../Data/csv_data/trump_facebook.tsv"
status_updates = read_csv(filename, delimiter="\t")
status_updates[0:2]
这是错误信息
IndexError Traceback (most recent call
last)
<ipython-input-16-352e8f130d5d> in <module>
27 filename = "../Data/csv_data/trump_facebook.tsv"
---> 28 status_updates = read_csv(filename, delimiter="\t")
29 status_updates[0:2]
<ipython-input-16-352e8f130d5d> in read_csv(input_file, delimiter)
9
10 dict_row = {"link_name": columns [0],
---> 11 "num_angrys": columns [1],
12 "num_comments":columns[2],
13 "num_hahas": columns [3],
IndexError: list index out of range
输出:
[{'link_name': 'link_name',
'num_angrys': 'num_angrys',
'num_comments': 'num_comments',
'num_hahas': 'num_hahas',
'num_likes': 'num_likes',
'num_loves': 'num_loves',
'num_reactions': 'num_reactions',
'num_sads': 'num_sads',
'num_shares': 'num_shares',
'num_wows': 'num_wows',
'status_id': 'status_id',
'status_link': 'status_link',
'status_message': 'status_message',
'status_published': 'status_published',
'status_type': 'status_type'},
{'link_name': 'Timeline Photos',
'num_angrys': '7',
'num_comments': '543',
'num_hahas': '17',
'num_likes': '6178',
'num_loves': '572',
'num_reactions': '6813',
'num_sads': '0',
'num_shares': '359',
'num_wows': '39',
'status_id': '153080620724_10157915294545725',
'status_link': 'https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10157915294545725/?type=3',
'status_message': 'Beautiful evening in Wisconsin- THANK YOU for your
incredible support tonight! Everyone get out on November 8th - and VOTE! LETS
MAKE AMERICA GREAT AGAIN! -DJT',
'status_published': '10/17/2016 20:56:51',
'status_type': 'photo'}]
我可以轻松地将status_update[0:2]替换为[1:3],但必须有一种更优雅的方法来删除标题行,这样我就不必担心每次调用此函数时都会使用1索引。谢谢你的帮助 csv文件是什么样子的。我看到函数调用使用delimeter作为
\t
,但实际代码总是使用,
您也可以考虑Python <代码> CSV 模块。
用熊猫怎么样?只需1行即可读取数据行
columns=row.split(“,”)
应该是columns=row.split(delimiter)
,但通常您应该使用(您甚至导入了csv
!)@Gsk我尝试了delimiter,但我的输出显示错误“nonetype”对象是不可订阅的。是的,我知道!但是我不允许使用csv.reader T_T你能用新的输出更新代码吗?你在哪里写了分隔符?Gsk我已经解决了!现在的问题是,当我打印status\u update[0:2]时,它会打印标题,我可以通过打印status\u update[1:3]轻松修复它,但我确信有一种更干净的方法可以做到这一点,这样我就不必每次调用此函数时都记得打印[1:~]。发布更新的代码!这看起来更像是一个评论,而不是一个答案。也许可以尝试提出他的问题的解决方案和一个小的使用示例csv.reader
?我不允许使用csv模块!我必须自己做code@Gsk,我更愿意发表评论,但当时我没有足够的声誉。谢谢你的建议。
[{'link_name': 'link_name',
'num_angrys': 'num_angrys',
'num_comments': 'num_comments',
'num_hahas': 'num_hahas',
'num_likes': 'num_likes',
'num_loves': 'num_loves',
'num_reactions': 'num_reactions',
'num_sads': 'num_sads',
'num_shares': 'num_shares',
'num_wows': 'num_wows',
'status_id': 'status_id',
'status_link': 'status_link',
'status_message': 'status_message',
'status_published': 'status_published',
'status_type': 'status_type'},
{'link_name': 'Timeline Photos',
'num_angrys': '7',
'num_comments': '543',
'num_hahas': '17',
'num_likes': '6178',
'num_loves': '572',
'num_reactions': '6813',
'num_sads': '0',
'num_shares': '359',
'num_wows': '39',
'status_id': '153080620724_10157915294545725',
'status_link': 'https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10157915294545725/?type=3',
'status_message': 'Beautiful evening in Wisconsin- THANK YOU for your
incredible support tonight! Everyone get out on November 8th - and VOTE! LETS
MAKE AMERICA GREAT AGAIN! -DJT',
'status_published': '10/17/2016 20:56:51',
'status_type': 'photo'}]
1. read first string of csv file as header
2. construct mapping "header-row" using `zip()` function
def row_preprocess(row, delimiter='\t'):
return row.strip('\n').split(delimiter)
def read_csv(path_to_file, delimiter='\t'):
csv_data = []
with open(path_to_file, 'r') as f:
column_values = row_preprocess(next(f), delimiter)
for row in f:
row_values = row_preprocess(row, delimiter)
mapping = dict(zip(column_values, row_values))
csv_data.append(mapping)
return csv_data