Python在CSV文件中不计算表头
我有一段Python代码,用来编辑一个CSV文件中的某一列。它的功能是去掉第五行中整数的零,然后如果这个整数是3或更小,就会在前面加一个零,这样就能保证这个数字总共有四位数或更多。
我遇到的问题是,它不喜欢标题行,因为标题行不是整数。有没有人知道怎么保持标题行不变,同时调整代码,让它不去看CSV文件的第一行呢?
以下是代码:
import csv
import re
import os
import sys
with open('', 'r') as infile, open('', 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
#firstline = True
#for row in outfile:
# if outfile:
# firstline = False
for row in reader:
# strip all 0's from the front
stripped_value = re.sub(r'^0+', '', row[5])
# pad zeros on the left to smaller numbers to make them 4 digits
row[5] = '%04d'%int(stripped_value)
writer.writerow(row)
2 个回答
0
你可以使用异常处理器。使用 try
是非常便宜的;因为你只需要一个头部,所以更复杂的 except
不会被调用太多次,从而不会影响性能。另外,这样你也能很好地处理后面出现的非数字行。
for row in reader:
# strip all 0's from the front
stripped_value = re.sub(r'^0+', '', row[5])
# pad zeros on the left to smaller numbers to make them 4 digits
try:
row[5] = '%04d'%int(stripped_value)
except ValueError:
pass # Or do something, to avoid passing it silently
writer.writerow(row)
你的代码片段需要正确的缩进:
import csv
import re
import os
import sys
with open('', 'r') as infile, open('', 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
# strip all 0's from the front
stripped_value = re.sub(r'^0+', '', row[5])
# pad zeros on the left to smaller numbers to make them 4 digits
try:
row[5] = '%04d'%int(stripped_value)
except ValueError:
pass # Or do something, to avoid passing it silently
writer.writerow(row)
1
在循环之前加上这个:
# Python 2.x
writer.writerow(reader.next())
# Python 3.x
writer.writerow(next(reader))
这样可以获取第一行的数据并返回。然后你就可以把它写入输出中。
不过,我觉得你应该让循环里面的代码能够处理那一列中不是数字的情况(就像Al.Sal的回答里说的那样)。