你好我有一个csv文件,我需要用python删除零:
python中的第6列第5列默认为7位数。用这个
AFI12001,01,C-,201405,P,0000430,2,0.02125000,US,60.0000
AFI12001,01,S-,201404,C,0001550,2,0.03500000,US,30.0000
我需要删除前面的零,然后我需要添加零或零以确保它总共有4位数
所以我需要它看起来像这样:
AFI12001,01,C-,201405,P,0430,2,0.02125000,US,60.0000
AFI12001,01,S-,201404,C,1550,2,0.03500000,US,30.0000
此代码添加了零
import csv
new_rows = []
with open('csvpatpos.csv','r') as f:
csv_f = csv.reader(f)
for row in csv_f:
new_row = ""
col = 0
print row
for x in row:
col = col + 1
if col == 6:
if len(x) == 3:
x = "0" + x
new_row = new_row + x + ","
print new_row
但是,我在删除前面的零时遇到问题。
答案 0 :(得分:2)
将列转换为int,然后将其转换为您想要的任何格式的字符串。
row[5] = "%04d" % int(row[5])
答案 1 :(得分:1)
with open('input') as in_file:
csv_reader = csv.reader(in_file)
for row in csv_reader:
stripped_data = row[5].lstrip('0')
new_data = stripped_data.zfill(4)
print new_data
打印:
0430
1550
该行:
stripped_data = row[5].lstrip('0')
摆脱了左边的所有零。这一行:
new_data = stripped_data.zfill(4)
用零填充前面,使总位数为4.
希望这有帮助。
答案 2 :(得分:1)
您可以在.lstrip()
的几个步骤中执行此操作,然后找到生成的字符串长度,然后将4-len(s)
0添加到前面。但是,我认为使用regex
更容易。
with open('infilename', 'r') as infile:
reader = csv.reader(infile)
for row in reader:
stripped_value = re.sub(r'^0{3}', '', row[5])
产量
0430
1550
在正则表达式中,我们使用格式sub(pattern, substitute, original)
。模式细分是:
'^' - match start of string
'0{3}' - match 3 zeros
你说第6列中的所有字符串都有7个数字,你想要4,所以用空字符串替换前3个字符串。
编辑:如果您想要替换行,我只会将其写入新文件:
with open('infilename', 'r') as infile, open('outfilename', 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
row[5] = re.sub(r'^0{3}', '', row[5])
writer.writerow(row)
Edit2 :根据您的最新要求,我建议您执行以下操作:
with open('infilename', 'r') as infile, open('outfilename', '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
row[5] = '%04d'%int(stripped_value)
writer.writerow(row)
鉴于以下数字,
['0000430', '0001550', '0013300', '0012900', '0100000', '0001000']
这会产生
['0430', '1550', '13300', '12900', '100000', '1000']
答案 3 :(得分:0)
你可以保留最后4个字符
columns[5] = columns[5][-4:]
例如
data = '''AFI12001,01,C-,201405,P,0000430,2,0.02125000,US,60.0000
AFI12001,01,S-,201404,C,0001550,2,0.03500000,US,30.0000'''
for row in data.splitlines():
columns = row.split(',')
columns[5] = columns[5][-4:]
print ','.join(columns)
结果
AFI12001,01,C-,201405,P,0430,2,0.02125000,US,60.0000
AFI12001,01,S-,201404,C,1550,2,0.03500000,US,30.0000
编辑:
使用csv
模块的代码 - 而不是data
来模拟文件。
import csv
with open('csvpatpos.csv','r') as f:
csv_f = csv.reader(f)
for row in csv_f:
row[5] = row[5][-4:]
print row[5] # print one element
#print ','.join(row) # print full row
print row # print full row