gpt4 book ai didi

python - 迭代列表和文件读取

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:46 25 4
gpt4 key购买 nike

import os
import sys
import textwrap

string123="00505661e418005056618f67080045000086000040004011c1bd1e1e1e321e1e1e3cc0e62118007200000800000000000100000c298a92ba000c29f914ea080045000054c757400040015a93464646144646461e080031e3470d000142bdaf5600000000cb27030000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637"

j=0
i = 0
string1234 = ''
while i < len(string123):
#print string123[i:i+2]
string1234 = string1234+' '+string123[i:i+2]
i+=2

final = string1234.strip()
final1= '\n'.join(textwrap.wrap(final, 47))
print final


f=open("final.txt","w")
f.write(final1)
f.close

f=open("final.txt","r")
g=f.readlines()

my_list=["000","001","002","003","004","005","007","008","009"]
line_new=""

for lines in g:
while j < len(my_list):
line_new = my_list[j]+" "+ lines
print line_new
lines+=1
j+=1

这个脚本实际上是将“S”中的每两个字符隔开并添加一个空格。然后它为每 47 个字符引入一个新行并将输出复制到 final.txt

final.txt 看起来像这样:

00 50 56 61 e4 18 00 50 56 61 8f 67 08 00 45 00
00 86 00 00 40 00 40 11 c1 bd 1e 1e 1e 32 1e 1e
1e 3c c0 e6 21 18 00 72 00 00 08 00 00 00 00 00
01 00 00 0c 29 8a 92 ba 00 0c 29 f9 14 ea 08 00
45 00 00 54 c7 57 40 00 40 01 5a 93 46 46 46 14
46 46 46 1e 08 00 31 e3 47 0d 00 01 42 bd af 56
00 00 00 00 cb 27 03 00 00 00 00 00 10 11 12 13
14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23
24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33
34 35 36 37

我想在第一行添加 000,然后在第二行添加 001,在第三行添加 003,依此类推。

因此,我创建了一个包含这些值的列表,并尝试将这两行与列表元素一起迭代。

my_list=["000","001","002","003","004","005","007","008","009"]
line_new=""

for lines in g:
while j < len(my_list):
line_new = my_list[j]+" "+ lines
print line_new
lines+=1
j+=1

但是它将列表中的所有元素附加到第一行。列表的第一个元素应该放在第一行的开头,第二个、第三个等等。

最佳答案

读取final.txt

from __future__ import print_function

with open("final.txt") as f:
for index, line in enumerate(f):
print('{:03d} {}'.format(index, line), end='')

输出:

000 00 50 56 61 e4 18 00 50 56 61 8f 67 08 00 45 00
001 00 86 00 00 40 00 40 11 c1 bd 1e 1e 1e 32 1e 1e
002 1e 3c c0 e6 21 18 00 72 00 00 08 00 00 00 00 00
003 01 00 00 0c 29 8a 92 ba 00 0c 29 f9 14 ea 08 00
004 45 00 00 54 c7 57 40 00 40 01 5a 93 46 46 46 14
005 46 46 46 1e 08 00 31 e3 47 0d 00 01 42 bd af 56
006 00 00 00 00 cb 27 03 00 00 00 00 00 10 11 12 13
007 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23
008 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33
009 34 35 36 37

首先写入final.txt

假设您想将数字添加到文件中,您可以这样做:

final = string1234.strip()

with open("final.txt","w") as f:
for index, line in enumerate(textwrap.wrap(final, 47)):
f.write('{:03d} {}\n'.format(index, line))

final.txt:

000 00 50 56 61 e4 18 00 50 56 61 8f 67 08 00 45 00
001 00 86 00 00 40 00 40 11 c1 bd 1e 1e 1e 32 1e 1e
002 1e 3c c0 e6 21 18 00 72 00 00 08 00 00 00 00 00
003 01 00 00 0c 29 8a 92 ba 00 0c 29 f9 14 ea 08 00
004 45 00 00 54 c7 57 40 00 40 01 5a 93 46 46 46 14
005 46 46 46 1e 08 00 31 e3 47 0d 00 01 42 bd af 56
006 00 00 00 00 cb 27 03 00 00 00 00 00 10 11 12 13
007 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23
008 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33
009 34 35 36 37

enumerate() 为您提供迭代的索引,从零开始。 format() 方法格式化输出行。 {:03d} 插入一个整数,前导零占据三个位置。以下 {}\n 获取该行的文本并在末尾添加一个换行符。

关于python - 迭代列表和文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35234858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com