在 Python 3.2 中将数字拆分为前 3 位和后 2 位等部分后,有什么方法可以显示前导零?我的脚本返回没有前导零的数字......
我有一个如下所示的 csv 文件:
Name,Code
blackberry,20001
wineberry,02002
rasberry,30000
blueberry,03010
我想要的输出:
Name,Code,Code1,Code2
blackberry,20001,200,01
wineberry,02002,020,02
rasberry,30000,300,00
blueberry,03010,030,10
我的脚本:
import csv
all = []
with open('aaa.csv','r') as csvinput:
with open('bbb.csv', 'w') as csvoutput:
reader = csv.reader(csvinput,delimiter=',')
writer = csv.writer(csvoutput,delimiter=",", lineterminator='\n')
row = next(reader)
row.append('Code1')
row.append('Code2')
all.append(row)
for row in reader:
row.append(row[1][0:2])
row.append(row[1][-2:])
all.append(row)
writer.writerows(all)
print(all)
上面的脚本返回:
Name,Code,Code1,Code2
blackberry,20001,200,1
wineberry,02002,20,2
rasberry,30000,300,0
blueberry,03010,30,10