2017-01-09 58 views
0

我是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 

而不是每个文件名称出现一次,然后进行校准,我立即打印所有文件,并在每个文件下进行多次校准。

我真的很感谢任何帮助。谢谢!

+0

多少行中输入文件?你可以在没有崩溃你的机器的情况下读取整个文件吗? – motanelu

+0

'outfile.write('[Back]'+ str(fileId)+'.bmp'+'\ r \ n')'将'fileId'修改为'line' – Bg1850

+0

您不应该提供''\ r \ n' '行尾。除非你用二进制模式编写(你不是),否则Python会自动将''\ n''转换为你的平台的正确结尾。 –

回答

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 
+0

有点解释性介绍会伤害没有人=) – slezica

+0

完成:)我认为是非常自我解释... – motanelu

+0

谢谢,我已经纠正它。唯一的问题是,不是每个文件名后都打印出一个CALIBRATION实例,而是获得7个相同的文件名(与文件名相同的编号)。 – user2711113

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)) 
+0

使用'str.format'而不是模运算符可能会更加Pythonic。 –

+0

非常感谢,这完美地解决了我的问题。 – user2711113