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

6 篇文章 0 订阅
读取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])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值