如何跳过头时处理一个csv文件使用Python?

我使用下面引用的代码使用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来解决这个问题,但它没有工作。

请帮我解决这个问题。

386416 次浏览

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

你想用next(reader)来跳过一行。

你的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)

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

给定"foo.csv"如下所示:

FirstColumn,SecondColumn
asdf,1234
qwer,5678

像这样使用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'])

受到马丁·彼得的回应的启发。

如果你只需要从csv文件中删除头文件,你可以更有效地工作,如果你使用标准的Python文件I/O库,避免使用CSV Python库:

with open("tmob_notcleaned.csv", "rb") as infile, open("tmob_cleaned.csv", "wb") as outfile:
next(infile)  # skip the headers
outfile.write(infile.read())

简单地用next()迭代一次

with open(filename) as file:


csvreaded = csv.reader(file)
header = next(csvreaded)


for row in csvreaded:
empty_list.append(row) #your csv list without header

或者在reader对象的末尾使用[1:]

with open(filename) as file:


csvreaded = csv.reader(file)
header = next(csvreaded)


for row in csvreaded[1:]:
empty_list.append(row) #your csv list without header