你好,我有一个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
我需要删除前面的0然后我需要添加一个或多个0以确保它的总数为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
这段代码添加了0
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
然而,我在删除前面的零时遇到了麻烦。
将列转换为整型,然后再转换为任意格式的字符串
row[5] = "%04d" % int(row[5])
您可以在.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']
可以使用lstrip()
和zfill()
方法。这样的:
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')
去掉左边所有的0。这一行:
new_data = stripped_data.zfill(4)
用0填充前面,使数字总数为4。
可以保留最后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