读取txt文件内容,并随机排序

读取txt文件
with open('match_image.txt') as f:
        content = f.readlines()

print(content)
['polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s7_p1_c3.jpg 1 7\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s8_p1_c1.jpg 1 8\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s8_p1_c3.jpg 1 8\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s9_p1_c1.jpg 1 9\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s9_p1_c3.jpg 1 9\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s10_p1_c1.jpg 1 10\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s10_p1_c3.jpg 1 10\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s11_p1_c1.jpg 1 11\n',
 'polyu\\p1\\s7_p1_c1.jpg 1 7 polyu\\p1\\s11_p1_c3.jpg 1 11\n',
.......
.......
.......
......]

随机打乱
import random

random.shuffle(content) #随机排序
print(content)
['polyu\\p4\\s16_p4_c1.jpg 4 16 polyu\\p13\\s29_p13_c3.jpg 13 29\n',
 'polyu\\p2\\s18_p2_c1.jpg 2 18 polyu\\p8\\s7_p8_c1.jpg 8 7\n',
 'polyu\\p12\\s18_p12_c1.jpg 12 18 polyu\\p20\\s28_p20_c3.jpg 20 28\n',
 'polyu\\p1\\s16_p1_c1.jpg 1 16 polyu\\p20\\s16_p20_c3.jpg 20 16\n',
 'polyu\\p17\\s7_p17_c3.jpg 17 7 polyu\\p20\\s25_p20_c3.jpg 20 25\n',
 'polyu\\p15\\s12_p15_c1.jpg 15 12 polyu\\p26\\s9_p26_c1.jpg 26 9\n',
 'polyu\\p2\\s26_p2_c3.jpg 2 26 polyu\\p3\\s13_p3_c3.jpg 3 13\n',
 'polyu\\p6\\s25_p6_c1.jpg 6 25 polyu\\p24\\s21_p24_c1.jpg 24 21\n',
 'polyu\\p8\\s30_p8_c1.jpg 8 30 polyu\\p9\\s18_p9_c3.jpg 9 18\n',
 'polyu\\p4\\s20_p4_c1.jpg 4 20 polyu\\p23\\s19_p23_c3.jpg 23 19\n',
......
......]

写入txt文件
match_image_random = open("match_image_random.txt","w")
for i in range(778042):
    match_image_random.write(content[i])

猜你喜欢

转载自blog.csdn.net/qq_38640439/article/details/81235870
今日推荐