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

时间:2017-01-09 21:28:32

标签: python multiple-files

我是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 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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