Python系列写作问题
我让这段代码在文本文件中编写一个系列Python系列写作问题,python,list,formatting,Python,List,Formatting,我让这段代码在文本文件中编写一个系列 bin = int("377731") with open('{0}/{0}.txt'.format(bin)) as fine: for cc in fine and i in range(1000): name = cc[6:11] tg = open('{0}/{1}.txt'.format(bin,name), 'a') tg.write('{0}{1:03}'.format(cc,i))
bin = int("377731")
with open('{0}/{0}.txt'.format(bin)) as fine:
for cc in fine and i in range(1000):
name = cc[6:11]
tg = open('{0}/{1}.txt'.format(bin,name), 'a')
tg.write('{0}{1:03}'.format(cc,i))
tg.close()
我希望这段代码能够像这样编写输出
37773100000000
37773100000001
...
37773100000
000
37773100000
001
...
但是当我执行代码时,输出显示如下
37773100000000
37773100000001
...
37773100000
000
37773100000
001
...
为什么代码是这样写的?
错误在哪里?当您在
fine
(for cc in fine
)上迭代时,您得到的是带有换行符的行,因此换行符在cc
和i
之间
如果显示它,您将看到:
>>> cc
'37773100000\n'
可以使用以下方法修剪线:
cc = cc.strip()
377731.txt文件的内容是什么?您好,@njzk2内容是两行37773100000 37773100001行中的每个数字不要将bin用作变量名。bin()返回整数或长整数的二进制表示形式。我很惊讶代码居然能正常工作
对于cc in fine和i in range(1000):
可能不会执行您认为它可以执行的任何操作。