import itertools
with open('zidian.txt', 'w') as z:
with open('file1.txt') as f1, open('file2.txt') as f2:
for a, b in itertools.product(f1, f2):
a, b = a.strip(), b.strip()
print(a+b, file=z)
切割輸出的做法:
import itertools
with open('file2.txt') as f2:
for key, group in itertools.groupby(enumerate(f2), lambda t: t[0]//5):
with open('file1.txt') as f1, open('zidian-{}.txt'.format(key), 'w') as z:
for a, (_, b) in itertools.product(f1, group):
a, b = a.strip(), b.strip()
print(a+b, file=z)
稍微說一下你原本代碼的一些問題:
f = open('zidian.txt','w') 你在這裡 open 了文件可是卻忘記關閉了, 讀寫文件還是使用 with 的作法會比較好
names = []
with open('file1.txt','r') as username:
for line in username.readlines():
names.append(line)
list = []
with open('file2.txt','r') as dict:
for line in dict.readlines():
list.append(line)
for i in range(len(line) / 5):
f = open('zidian' + str(i + 1) + '.txt', 'w')
for j in range(5):
for name in names:
f.write(user.strip() + line[i * 5 + j] + '\n')
f.close()
# 把除5的余数,即剩下的最后几行再写一个文件,代码不写了
with open('file2') as file2_handle:
passwords = file2_handle.readlines()
# 当然了,就如楼上所说,用readlines不好,但是这不是绝对的,在你的文件没有大到内存吃不消的情况下,readlines会显著提高程序的性能(这句话是有问题的,前提是你没拿读文件的IO时间做其他的事)
# 在我看来,几百万行的文件,那都不是事,我用python读取10G以上的文件都是常有的事
# 当然了,尽量不要用readlines,这里只是为了我实现下面的算法方便
with open('file1') as file1_handle:
name_password_dict = ['%s%s' % (line.rstrip(), passwords[i%len(passwords)]) for i, line in enumerate(file1_handle)]
# 有了name_password_dict还不是想干嘛干嘛,不管是分文件其他是什么的
這裡是不求切割文件的作法,
itertools.product可以幫你完成地更簡潔:切割輸出的做法:
稍微說一下你原本代碼的一些問題:
f = open('zidian.txt','w')你在這裡 open 了文件可是卻忘記關閉了, 讀寫文件還是使用 with 的作法會比較好dict.readlines(), 若非萬不得已, 不要使用readlines, 千萬記得!! 請參考這篇文章 文本格式轉換代碼優化另外,
dic或dict這個字, 在 python 中有著獨特的意義, 稍微有點經驗的 python programmer 都會認為他們是 python dictionary, 這容易造成誤會我回答過的問題: Python-QA
把file2每行存到一个list里面,然后每次从list里面拿五个就行了啊
手头没有python,代码纯手写估计有错误。理解思想即可
@dokelung 的itertools.cycle是个妙用,我还有更好的方法:
简单来说增加一个计数器line,每匹配一组值line += 1,line为5的时候关闭文件,打开新的文件并置line为0.