left padding with python



我有以下100000个条目的数据和链接组合

dn:id=2150fccc-beb8-42f8-b201-182a6bf5ddfe,ou=test,dc=com
link:545214569
dn:id=ffa55959-457d-49e6-b4cf-a34eff8bbfb7,ou=test,dc=com
link:32546897
dn:id=3452a4c3-b768-43f5-8f1e-d33c14787b9b,ou=test,dc=com
link:6547896541

我试图在python 2.7中编写一个程序,如果链接的值小于10,则添加左填充零。

,

545214569  --> 0545214569
32546897   --> 0032546897

你能告诉我下面的程序有什么问题吗?

with open("test.txt", "r") as f:
line=f.readline()
line1=f.readline()
wordcheck = "link"
wordcheck1= "dn"
for wordcheck1 in line1:
with open("pad-link.txt", "a") as ff:
for wordcheck in line:
with open("pad-link.txt", "a") as ff:
key, val = line.strip().split(":")
val1  = val.strip().rjust(10,'0')
line = line.replace(val,val1)
print (line)
print (line1)
ff.write(line1 + "n")
ff.write('%s:%s n' % (key, val1))

在Python中填充值的常用Python方法是使用字符串格式化和Format Specification Mini Language

link = 545214569
print('{:0>10}'.format(link))

你的for wordcheck1 in line1:for workcheck in line:做的不是你想的那样。它们一次迭代一个字符并将该字符赋值给workcheck变量。

如果您只想将输入文件更改为前导零,则可以简化为:

import re
# Read the whole file into memory
with open('input.txt') as f:
data = f.read()
# Replace all instances of "link:<digits>", passing the digits to a function that
# formats the replacement as a width-10 field, right-justified with zeros as padding.
data = re.sub(r'link:(d+)', lambda m: 'link:{:0>10}'.format(m.group(1)), data)
with open('output.txt','w') as f:
f.write(data)

output.txt:

dn:id=2150fccc-beb8-42f8-b201-182a6bf5ddfe,ou=test,dc=com
link:0545214569
dn:id=ffa55959-457d-49e6-b4cf-a34eff8bbfb7,ou=test,dc=com
link:0032546897
dn:id=3452a4c3-b768-43f5-8f1e-d33c14787b9b,ou=test,dc=com
link:6547896541

我不知道你为什么要打开很多次。无论如何,打开1次,然后为每一行,分成:。列表中的最后一个元素是数字。然后你知道数字的长度应该是多少,比如150,然后使用zfill填充0。然后使用join

将这些行放回去
for line in f.readlines():
words = line.split(':')
zeros = 150-len(words[-1])
words[-1] = words[-1].zfill(zeros)
newline = ':'.join(words)
# write this line to file 

相关内容

  • 没有找到相关文章

最新更新