如何将文件的每一行按随机顺序写入新文件?

2021-06-13 13:52:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,已经为这个问题奋斗了一段时间。我想写一个函数,它从一个名为FileNames.txt的文件中获取一组BMP文件名,然后在一个名为config_1.cfg的新生成的文件中随机打印出来。你知道吗

FileNames.txt文件中的文件名如下所示:

E1I11D0.bmp
E1I13D0.bmp
E1I15D0.bmp
E1I17D0.bmp
E1I19D0.bmp
E1I1D0.bmp
E1I21D0.bmp

我想把它们写在一个新的config_1.cfg文件中,这个文件应该是这样的:

#Filename: config_1.cfg
#Subject: 1

-PRESENT 1
[Back] "E1I11D0.bmp"
[Connect] 0 1

-CALIBRATION 1
[Connect] 0 2

-PRESENT 2
[Back] "E1I1D0.bmp"
[Connect] 0 3

-CALIBRATION 2
[Connect] 0 4

-PRESENT 3
[Back] "E1I19D0.bmp"
[Connect] 0 5

.txt文件中的所有BMP文件名都需要按随机顺序写出。到目前为止,我已经尝试生成.cfg文件,如下所示:

import os
import random

curdir = os.getcwd()
os.chdir('/Users/UserName/Folder')

index = 1
count = 1

# Create output .cfg file

outfile = open('config_1' + '.cfg', 'w')

outfile.write('# Filename: config_1' + '\n');
outfile.write('#Subject: 1' + '\n');

#Get the BMP file names from the .txt file

with open('FileNames.txt', 'r') as fin:
    fileId = list(fin)

random.shuffle(fileId)

for line in fileId:

    outfile.write('-PRESENT ' + str(index) + '\n')
    outfile.write('[Back] ' + str(fileId) + '.bmp' + '\r\n')
    outfile.write('[Connect] 0 ' + str(index + 1) + '\r\n\r\n')
    index += 1

# Add SHUTTERCALIBRATION after each instance of BMP file name being printed

caliblock = 1

for line in fileId:
    outfile.write('-CALIBRATION ' + str(index) + '\r\n')
    outfile.write('[Connect] 0 ' + str(index + 1) + '\r\n\r\n')
    index += 1

但是,当我运行代码时,我在.cfg中得到的是:

# Filename: config_1
#Subject: 1
-PRESENT 1
[Back] ['E1_I1_3D0.bmp\r\n', 'E1_I1_5D0.bmp\r\n', 'E1_I1_D0.bmp\r\n',     'E1_I1_9D0.bmp\r\n', 'E1_I1_7D0.bmp\r\n', 'E1_I2_1D0.bmp\r\n',     'E1_I1_1D0.bmp\r\n'].bmp
[Connect] 0 2

-CALIBRATION 2
[Connect] 0 3

-CALIBRATION 2
[Connect] 0 3

-CALIBRATION 2
[Connect] 0 3

-CALIBRATION 2
[Connect] 0 3

-CALIBRATION 2
[Connect] 0 3

-CALIBRATION 2
[Connect] 0 3

-CALIBRATION 2
[Connect] 0 3

-PRESENT 2
[Back] ['E1_I1_3D0.bmp\r\n', 'E1_I1_5D0.bmp\r\n', 'E1_I1_D0.bmp\r\n',     'E1_I1_9D0.bmp\r\n', 'E1_I1_7D0.bmp\r\n', 'E1_I2_1D0.bmp\r\n',     'E1_I1_1D0.bmp\r\n'].bmp
[Connect] 0 3

不是每个文件名出现一次,然后进行一次校准,而是一次打印所有文件名,并在每个文件名下进行多次校准。你知道吗

我真的很感激你的帮助。谢谢您!你知道吗

2条回答
网友
1楼 ·

代码中有一个输入错误,在遍历数组时,应该显示当前行,而不是整个数组。你知道吗

for line in fileId:

    outfile.write('-PRESENT ' + str(index) + '\n')
    outfile.write('[Back] ' + str(fileId) + '.bmp' + '\r\n')
    ________________________________^ "fileId" should be "line"
    outfile.write('[Connect] 0 ' + str(index + 1) + '\r\n\r\n')
    index += 1
网友
2楼 ·

我会像这样重写你的代码:

import random

with open("FileNames.txt", "r") as f:
    fnames = f.read().split()
random.shuffle(fnames)

chunk_format = """
-PRESENT %s
[Back] "%s"
[Connect] 0 %s

-CALIBRATION %s
[Connect] 0 %s
"""

outname = "config_1.cfg"
with open(outname, "w") as f:
    f.write("#Filename: %s\n" % outname)
    f.write("Subject: 1\n")
    for i, fname in enumerate(fnames):
        nchunk = i + 1
        nconnect = 2 * i + 1
        f.write(chunk_format % (nchunk, fname, nconnect, nchunk, nconnect+1))

相关问题