使用Python编辑CSV文件时跳过标题

发布于 2021-02-02 23:13:11

我正在使用以下引用的代码使用Python编辑CSV。代码中调用的函数构成了代码的上部。

问题:我希望下面引用的代码从第二行开始编辑csv,我希望它排除包含标题的第一行。现在,它仅在第一行上应用函数,并且我的标题行正在更改。

in_file = open("tmob_notcleaned.csv", "rb")
reader = csv.reader(in_file)
out_file = open("tmob_cleaned.csv", "wb")
writer = csv.writer(out_file)
row = 1
for row in reader:
    row[13] = handle_color(row[10])[1].replace(" - ","").strip()
    row[10] = handle_color(row[10])[0].replace("-","").replace("(","").replace(")","").strip()
    row[14] = handle_gb(row[10])[1].replace("-","").replace(" ","").replace("GB","").strip()
    row[10] = handle_gb(row[10])[0].strip()
    row[9] = handle_oem(row[10])[1].replace("Blackberry","RIM").replace("TMobile","T-Mobile").strip()
    row[15] = handle_addon(row[10])[1].strip()
    row[10] = handle_addon(row[10])[0].replace(" by","").replace("FREE","").strip()
    writer.writerow(row)
in_file.close()    
out_file.close()

我试图通过将row变量初始化为来解决此问题,1但没有成功。

请帮助我解决这个问题。

关注者
0
被浏览
110
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    你的reader变量是可迭代的,通过循环它可以检索行。

    要使其在循环前跳过一项,只需调用next(reader, None)并忽略返回值即可。

    你还可以稍微简化代码;使用打开的文件作为上下文管理器可以自动关闭它们:

    with open("tmob_notcleaned.csv", "rb") as infile, open("tmob_cleaned.csv", "wb") as outfile:
       reader = csv.reader(infile)
       next(reader, None)  # skip the headers
       writer = csv.writer(outfile)
       for row in reader:
           # process each row
           writer.writerow(row)
    
    # no need to close, the files are closed automatically when you get to this point.
    

    如果你想将标头写入未处理的输出文件中,也很容易,请将输出传递next()writer.writerow()

    headers = next(reader, None)  # returns the headers or `None` if the input is empty
    if headers:
        writer.writerow(headers)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看