我想在文本文件中打印一个巨大的数字列表,每行应该有5个数字。每个数字都是20位数。
下面是数字列表(这个巨大的数字列表叫做a1)
3.1948343452787551598e-08 8.2713170790435268456e-08 9.2997407466072783263e-08 2.2201974063356576525e-08 4.7184351703512537954e-08 6.4869237006439418830e-08 5.6105657006093290136e-08 2.5869788723656166592e-08 3.2336150408719792489e-08 1.3711755095387755690e-08
0.8466139114156927980e-08 8.6552265489125695952e-08 1.6410391288046437544e-08 6.4151550079023113112e-08 9.9077814220256712144e-08 4.8228459783414887112e-08 1.6503915571969716123e-08 5.0759765831642425139e-08 1.4163943549368568877e-08 0.9171874262950143401e-08
2.2924892903408382299e-08 8.0910403085272639979e-08 0.1150909967310807486e-08 1.7800414806457459589e-08 9.9156327409347916878e-08 0.7474308272820071357e-08 4.8164050539957729897e-08 7.2570119501482690351e-08 5.5760621634989852061e-08 3.3409017307778332564e-08
4.7565190913441753115e-08 5.1557086343029653458e-08 6.3693801211606923532e-08 7.8897108738118126312e-08 9.4510334836961360301e-08 7.4880836459074272283e-08 4.9268327788150989424e-08 1.8466453757372403026e-08 6.2011855819003971679e-08 2.0783930984602632852e-08
0.9917554541229221025e-08 7.5224425124573985562e-08 2.6181377446191134266e-08 8.9009270149441469026e-08 7.7175189573912221707e-08 2.2170581996655944683e-08 3.2639560946849215485e-08 7.0901354279344938844e-08 5.5997044308008179085e-08 9.8907609461314007943e-08
3.1948343452787551598e-08 8.2713170790435268456e-08 9.2997407466072783263e-08 2.2201974063356576525e-08 4.7184351703512537954e-08 6.4869237006439418830e-08 5.6105657006093290136e-08 2.5869788723656166592e-08 3.2336150408719792489e-08 1.3711755095387755690e-08
0.8466139114156927980e-08 8.6552265489125695952e-08
这是我到目前为止所做的。
with open('testfile5.txt','w') as testfile5:
testfile5.write(a1)
testfile5.close()
答案 0 :(得分:0)
ddd.txt
:
3.1948343452787551598e-08 8.2713170790435268456e-08 9.2997407466072783263e-08 2.2201974063356576525e-08 4.7184351703512537954e-08 6.4869237006439418830e-08 5.6105657006093290136e-08 2.5869788723656166592e-08 3.2336150408719792489e-08 1.3711755095387755690e-08
0.8466139114156927980e-08 8.6552265489125695952e-08 1.6410391288046437544e-08 6.4151550079023113112e-08 9.9077814220256712144e-08 4.8228459783414887112e-08 1.6503915571969716123e-08 5.0759765831642425139e-08 1.4163943549368568877e-08 0.9171874262950143401e-08
2.2924892903408382299e-08 8.0910403085272639979e-08 0.1150909967310807486e-08 1.7800414806457459589e-08 9.9156327409347916878e-08 0.7474308272820071357e-08 4.8164050539957729897e-08 7.2570119501482690351e-08 5.5760621634989852061e-08 3.3409017307778332564e-08
4.7565190913441753115e-08 5.1557086343029653458e-08 6.3693801211606923532e-08 7.8897108738118126312e-08 9.4510334836961360301e-08 7.4880836459074272283e-08 4.9268327788150989424e-08 1.8466453757372403026e-08 6.2011855819003971679e-08 2.0783930984602632852e-08
0.9917554541229221025e-08 7.5224425124573985562e-08 2.6181377446191134266e-08 8.9009270149441469026e-08 7.7175189573912221707e-08 2.2170581996655944683e-08 3.2639560946849215485e-08 7.0901354279344938844e-08 5.5997044308008179085e-08 9.8907609461314007943e-08
3.1948343452787551598e-08 8.2713170790435268456e-08 9.2997407466072783263e-08 2.2201974063356576525e-08 4.7184351703512537954e-08 6.4869237006439418830e-08 5.6105657006093290136e-08 2.5869788723656166592e-08 3.2336150408719792489e-08 1.3711755095387755690e-08
0.8466139114156927980e-08 8.6552265489125695952e-08
解决方案:
with open('ddd.txt', 'r') as f:
nums = []
for line in f:
nums.extend(line.split())
with open('eee.txt', 'w') as f:
for i in range(0, len(nums), 5):
five_nums = ' '.join(nums[i:i + 5])
f.write('{}\n'.format(five_nums))
eee.txt
3.1948343452787551598e-08 8.2713170790435268456e-08 9.2997407466072783263e-08 2.2201974063356576525e-08 4.7184351703512537954e-08
6.4869237006439418830e-08 5.6105657006093290136e-08 2.5869788723656166592e-08 3.2336150408719792489e-08 1.3711755095387755690e-08
0.8466139114156927980e-08 8.6552265489125695952e-08 1.6410391288046437544e-08 6.4151550079023113112e-08 9.9077814220256712144e-08
4.8228459783414887112e-08 1.6503915571969716123e-08 5.0759765831642425139e-08 1.4163943549368568877e-08 0.9171874262950143401e-08
2.2924892903408382299e-08 8.0910403085272639979e-08 0.1150909967310807486e-08 1.7800414806457459589e-08 9.9156327409347916878e-08
0.7474308272820071357e-08 4.8164050539957729897e-08 7.2570119501482690351e-08 5.5760621634989852061e-08 3.3409017307778332564e-08
4.7565190913441753115e-08 5.1557086343029653458e-08 6.3693801211606923532e-08 7.8897108738118126312e-08 9.4510334836961360301e-08
7.4880836459074272283e-08 4.9268327788150989424e-08 1.8466453757372403026e-08 6.2011855819003971679e-08 2.0783930984602632852e-08
0.9917554541229221025e-08 7.5224425124573985562e-08 2.6181377446191134266e-08 8.9009270149441469026e-08 7.7175189573912221707e-08
2.2170581996655944683e-08 3.2639560946849215485e-08 7.0901354279344938844e-08 5.5997044308008179085e-08 9.8907609461314007943e-08
3.1948343452787551598e-08 8.2713170790435268456e-08 9.2997407466072783263e-08 2.2201974063356576525e-08 4.7184351703512537954e-08
6.4869237006439418830e-08 5.6105657006093290136e-08 2.5869788723656166592e-08 3.2336150408719792489e-08 1.3711755095387755690e-08
0.8466139114156927980e-08 8.6552265489125695952e-08
答案 1 :(得分:0)
假设a1是列表然后尝试: -
fl = open('testfile5.txt',"w")
count = 0
with open('ddd.txt','r') as f:
line = f.read()
line = line.strip().split()
for i in range(5,len(line)+1,5):
result = ''.join(str(line[count:i]))
fl.write(result+'\n')
count = i
fl.close()