DictWriter是否删除所有数据?

时间:2019-01-25 17:18:03

标签: python csv dictionary

我正在尝试编写一个脚本,该脚本使用字典来匹配CSV中成对的行,然后组合这些对。

当我尝试创建输出文件时,脚本现在可以工作到最后一节。而不是用成对的行重写文件,它只是删除文件中的所有信息。

fn = "test.csv"
import csv
with open(fn, "r", encoding = "UTF-8") as inputfile:
    rows = list(csv.DictReader(inputfile))
row_dict = {r["number"]: r for r in rows}
r_v_pairs = {}
for ref, row in row_dict.items(): 
    s1 = (ref, row["Obverse"])
    s2 = (row["Obverse"], ref)
    if s1 in r_v_pairs:
        r_v_pairs[s1].append(row)
    elif s2 in r_v_pairs:
        r_v_pairs[s2].append(row)
    else: 
        r_v_pairs[s1] = [row]
r_v_pairs = list(r_v_pairs.values())
def combine_rows(rows):
    if len(rows) < 2:
        pass
###there's a problem here if there is only one. it's probably a corrupted file.
    elif len(rows) > 2:
        pass
###there's a problem here if there are more than 2. it's probably a duplicate.
    else:
        new_row = {}
        r1, r2 = rows
        return new_row
new_rows = [combine_rows(rs) for rs in r_v_pairs]
new_rows = [r for r in new_rows if r]
with open("test.csv","w", encoding='utf-8') as output_file:
    writer = csv.DictWriter(output_file)
    writer.writerows(new_rows)

0 个答案:

没有答案