根據以下示例,我有兩個單詞表:
wordlist1.txt
aa
bb
cc
wordlist2.txt
11
22
33
我想從 wordlist2.txt 中取出每一行并將其放在 wordlist1.txt 中的每一行之后,然后將它們組合在 wordlist3.txt 中,如下所示:
aa
11
bb
22
cc
33
.
.
你能幫我看看怎么做嗎?謝謝!
uj5u.com熱心網友回復:
除了使用.splitlines(),您還可以直接遍歷檔案。這是代碼:
wordlist1 = open("wordlist1.txt", "r")
wordlist2 = open("wordlist2.txt", "r")
wordlist3 = open("wordlist3.txt", "w")
for txt1,txt2 in zip(wordlist1, wordlist2):
if not txt1.endswith("\n"):
txt1 ="\n"
wordlist3.write(txt1)
wordlist3.write(txt2)
wordlist1.close()
wordlist2.close()
wordlist3.close()
在第一個塊中,我們正在打開檔案。對于前兩個,我們使用"r"代表讀取,因為我們不想對檔案進行任何更改。我們可以省略它,因為“r”是 open 函式的默認引數。對于第二個,我們使用"w",代表寫入。如果該檔案尚不存在,它將創建一個新檔案。
接下來,我們zip在 for 回圈中使用該函式。它創建一個迭代器,其中包含作為引數提供的所有可迭代物件的元組。在這個回圈中,它將包含包含每一行的元組wordlist1.txt和一個wordlist2.txt。這些元組直接解包到變數txt1和txt2.
接下來我們使用 if 陳述句來檢查一行是否wordlist1.txt以換行符結尾。最后一行可能不是這種情況,因此需要檢查。我們不檢查第二行,因為最后一行沒有換行符沒有問題,因為它也將在結果檔案的末尾。
接下來,我們將文本寫入wordlist3.txt. 這意味著文本被附加到檔案的末尾。但是,在打開之前已經存在于檔案中的文本會丟失。
最后,我們關閉檔案。這樣做非常重要,否則可能無法保存某些進度,同時其他應用程式也無法使用該檔案。
uj5u.com熱心網友回復:
嘗試始終嘗試包括您嘗試過的內容。
但是,這是一個很好的起點。
def read_file_to_list(filename):
with open(filename) as file:
lines = file.readlines()
lines = [line.rstrip() for line in lines]
return lines
wordlist1= read_file_to_list("wordlist1.txt")
wordlist2= read_file_to_list("wordlist2.txt")
with open("wordlist3.txt",'w',encoding = 'utf-8') as f:
for x,y in zip(wordlist1,wordlist2):
f.write(x "\n")
f.write(y "\n")
檢查以下問題以獲得更多想法和理解:如何將檔案逐行讀取到串列中?
干杯
uj5u.com熱心網友回復:
打開 wordlist1.txt 和 wordlist2.txt 進行閱讀,打開 wordlist3.txt 進行寫作。然后它很簡單:
with open('wordlist3.txt', 'w') as w3, open('wordlist1.txt') as w1, open('wordlist2.txt') as w2:
for l1, l2 in zip(map(str.rstrip, w1), map(str.rstrip, w2)):
print(f'{l1}\n{l2}', file=w3)
uj5u.com熱心網友回復:
嘗試這個:
with open('wordlist1.txt', 'r') as f1:
f1_list = f1.read().splitlines()
with open('wordlist2.txt', 'r') as f2:
f2_list = f2.read().splitlines()
f3_list = [x for t in list(zip(f1, f2)) for x in t]
with open('wordlist3.txt', 'w') as f3:
f3.write("\n".join(f3_list))
uj5u.com熱心網友回復:
with open('wordlist1.txt') as w1,\
open('wordlist2.txt') as w2,\
open('wordlist3.txt', 'w') as w3:
for wordlist1, wordlist2 in zip(w1.readlines(), w2.readlines()):
if wordlist1[-1] != '\n':
wordlist1 = '\n'
if wordlist2[-1] != '\n':
wordlist2 = '\n'
w3.write(wordlist1)
w3.write(wordlist2)
uj5u.com熱心網友回復:
干得好 :)
with open('wordlist1.txt', 'r') as f:
file1 = f.readlines()
with open('wordlist2.txt', 'r') as f:
file2 = f.readlines()
with open('wordlist3.txt', 'w') as f:
for x in range(len(file1)):
if not file1[x].endswith('\n'):
file1[x] = '\n'
f.write(file1[x])
if not file2[x].endswith('\n'):
file2[x] = '\n'
f.write(file2[x])
uj5u.com熱心網友回復:
打開 wordlist 1 和 2 并進行行配對,用換行符分隔每對,然后將所有對連接在一起并再次用換行符分隔。
# paths
wordlist1 = #
wordlist2 = #
wordlist3 = #
with open(wordlist1, 'r') as fd1, open(wordlist2, 'r') as fd2:
out = '\n'.join(f'{l1}\n{l2}' for l1, l2 in zip(fd1.read().split(), fd2.read().split()))
with open(wordlist3, 'w') as fd:
fd.write(out)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/486476.html
