[英]concat lines from several .txt file using python?
我在 Windows 10 上使用 python 3.6,我有一項任務是從 1.txt 獲取數據並將它們與一些字符串連接起來,然后將結果放在 2.txt 文件中。
這是我的代碼:
full_url = "https://mysite/images/pic_person/small/"
#file read from
pic_name = open("test.txt","r")
#file write to it
full_name = open("full_name.txt","a")
while True:
line = pic_name.readline()
link = line+full_url
print(link)
full_name.write(link)
if ("" == line):
print("file finished")
break;
pic_name.close()
full_name.close()
執行代碼后,它給了我這個結果:
p100003.jpg https://mysite/images/pic_person/small/p100026.jpg https://mysite/images/pic_person/small/p100951.jpg https://mysite/images/pic_person/small/p100970.jpg https://mysite/images/pic_person/small/p101144.jpghttps://mysite/images/pic_person/small/https://mysite/images/pic_person/small/
除了結果將是這樣的:
https://mysite/images/pic_person/small/p100026.jpg https://mysite/images/pic_person/small/p100951.jpg https://mysite/images/pic_person/small/p100970.jpg https://mysite/images/pic_person/small/p101144.jpg
文件 test.txt 包含以下幾行:
p100026.jpg
p100951.jpg
p100970.jpg
p101144.jpg
要在底部附加文件,您需要在打開的文件中使用 'a' 標志並將“\\n”作為輸入
full_url = "https://mysite/images/pic_person/small/"
# file read from test.txt
f = open("test.txt", "r")
# iterating each file on pict and write it
with open("text2.txt", "a") as file_object:
for item in f:
file_object.write(full_url+item.splitlines()[0]+"\n")
f.close()
你可以更換線
full_name.write(link)
和
full_name.writeline(link)
或者,您可以自己編寫“換行符”(將其視為模擬按 Enter)
full_name.write(link + '\r\n')
你可以使用這個方法
full_url = "https://mysite/images/pic_person/small/"
with open("test.txt") as pic_name:
links = [full_url + line.rstrip() for line in pic_name]
with open("full_name.txt","w") as name:
name.write("\n".join(links))
或者
with open("test.txt") as pic_name:
with open("full_name.txt","a") as name:
for line in pic_name:
name.write(full_url + line.rstrip() + "\n")
full_name.txt
輸出
https://mysite/images/pic_person/small/p100026.jpg
https://mysite/images/pic_person/small/p100951.jpg
https://mysite/images/pic_person/small/p100970.jpg
https://mysite/images/pic_person/small/p101144.jpg
with open(file, mode=["r", "w", "w+"]) as <file_handler>:
不是open()
和<fhandler>.close()
。 如果發生錯誤,文件流會在with
版本中自動關閉with
而在后一個版本中則不會。 所以with
更安全和健壯。for
循環 - 然后不需要對文件結尾 ( eof
) 進行顯式測試。 它逐行讀取。 您的line == ""
測試對於這種情況可能沒問題 - 但對於另一種情況可能不行。 使用 for 循環是更通用的解決方案。 此外——更重要的是——內存在任何時間點都只被fin
文件的一行占用。 所以內存占用很小。所以這個問題最pythonic的方法是:
infile = "test.txt"
outfile = "full_name.txt"
full_url = "https://mysite/images/pic_person/small/"
with open(infile, "r") as fin:
with open(outfile, "a") as fout:
for line in fin:
fout.write(full_url + line + "\n")
# or: print(full_url + line, end="\n", file=fout)
如果您希望像示例中那樣詳細打印,請執行以下操作:
infile = "test.txt"
outfile = "full_name.txt"
full_url = "https://mysite/images/pic_person/small/"
with open(infile, "r") as fin:
with open(outfile, "a") as fout:
for line in fin:
result_line = full_url + line
fout.writeline(result_line)
print(result_line)
print("file finished")
你可以試試這個。
full_url = "https://mysite/images/pic_person/small/"
outfile = "fullname.txt"
infile = "test.txt"
with open(outfile, "a") as w:
with open(infile, "r") as r:
for line in r: # read each line in the input file
w.write(f"{full_url}{line}") # write to output file
p100026.jpg
p100951.jpg
p100970.jpg
p101144.jpg
https://mysite/images/pic_person/small/p100026.jpg
https://mysite/images/pic_person/small/p100951.jpg
https://mysite/images/pic_person/small/p100970.jpg
https://mysite/images/pic_person/small/p101144.jpg
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.