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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14257373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:53:15  来源:igfitidea点击:

Skip the headers when editing a csv file using Python

pythonpython-2.7csv

提问by

I am using below referred code to edit a csv using Python. Functions called in the code form upper part of the code.

我正在使用下面引用的代码来使用 Python 编辑 csv。代码中调用的函数构成了代码的上半部分。

Problem: I want the below referred code to start editing the csv from 2nd row, I want it to exclude 1st row which contains headers. Right now it is applying the functions on 1st row only and my header row is getting changed.

问题:我希望下面引用的代码从第二行开始编辑 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()

I tried to solve this problem by initializing rowvariable to 1but it didn't work.

我试图通过初始化row变量来解决这个问题,1但它没有用。

Please help me in solving this issue.

请帮我解决这个问题。

采纳答案by Martijn Pieters

Your readervariable is an iterable, by looping over it you retrieve the rows.

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

To make it skip one item before your loop, simply call next(reader, None)and ignore the return value.

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

You can also simplify your code a little; use the opened files as context managers to have them closed automatically:

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

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.

If you wanted to write the header to the output file unprocessed, that's easy too, pass the output of next()to writer.writerow():

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

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

回答by Katriel

Doing row=1won't change anything, because you'll just overwrite that with the results of the loop.

这样做row=1不会改变任何东西,因为你只会用循环的结果覆盖它。

You want to do next(reader)to skip one row.

你想next(reader)跳过一行。

回答by Chad Zawistowski

Another way of solving this is to use the DictReader class, which "skips" the header row and uses it to allowed named indexing.

解决这个问题的另一种方法是使用 DictReader 类,它“跳过”标题行并使用它来允许命名索引。

Given "foo.csv" as follows:

鉴于“foo.csv”如下:

FirstColumn,SecondColumn
asdf,1234
qwer,5678

Use DictReader like this:

像这样使用 DictReader:

import csv
with open('foo.csv') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        print(row['FirstColumn'])  # Access by column header instead of column number
        print(row['SecondColumn'])