如何通过python脚本对csv文件进行排序?
我在谷歌和这个网站上读过类似问题的其他答案,但没有一个在我的脚本中起作用 我需要按照py脚本的第三列对如何通过python脚本对csv文件进行排序?,python,sorting,csv,Python,Sorting,Csv,我在谷歌和这个网站上读过类似问题的其他答案,但没有一个在我的脚本中起作用 我需要按照py脚本的第三列对csv\u文件中的信息进行排序。然后,使用排序后的信息,查找重复项并删除它们,但将计数添加到csv_文件中 for ip in open("lists.txt"): with open("csv_file.csv", "a") as csv_file: csv_file.write("\r IP:" + ip.strip() + ", Count, A, B, C \r"
csv\u文件中的信息进行排序。然后,使用排序后的信息,查找重复项并删除它们,但将计数添加到csv_文件中
for ip in open("lists.txt"):
with open("csv_file.csv", "a") as csv_file:
csv_file.write("\r IP:" + ip.strip() + ", Count, A, B, C \r")
for line in open("data.txt"):
new_line = line.split()
if "word" in new_line:
if "word"+ip.strip() in new_line:
csv_file.write(ip.strip() + ", " + new_line[10].replace("word=", ", ") + new_line[12].replace("word=", ", "))
try:
csv_file.write(new_line[14].replace("word=", ", "))
except IndexError:
pass
csv_file.write("\r")
with open("csv_file.csv", "r") as inputfile:
reader = csv.reader(inputfile)
headers = next(reader)
for row in reader:
key = (row[0], row[1:])
if key not in rows:
rows[key] = row + [0,]
rows[key][-1] += 1
我不知道这为什么不起作用,返回的错误如下:
TypeError: unhashable type: 'list'
问题:如何通过py脚本按第3列排序、删除重复项并将重复计数添加到我的csv\u文件中?如果我没有弄错,将打开“a”标记以写入此行:
with open("csv_file.csv", "a") as inputfile:
这意味着你是为了写作而开放的,而不是为了阅读。您应该使用“r”或“+”您的意思是reader.next()
,而不是next(reader)
csv文件有多大?@yosukesabai:为什么reader.next()
@比尔:对不起,我得把它收回去。。。。next()应该可以。正如本杰明的回答所说,打开“a”可能是个问题。你刚才不是说过吗?这和前一个有什么不同?