def readfile(filename,mode="rt"):
head = "<li><a href="""
center =""" target="_blank">"""
end ="<br></a></li>"
with open(filename,mode) as fin:
for line in fin:
link = line
length_link = len(link)
twitter_name_after = link[twitter_name_before+5:]
full_code = head+link+center+"@"+twitter_name_after+end
writefile("C:/Users/Johny The Overlord/Documents/Code/test_1.txt",full_code)
def writefile(filename,contents,mode="wt"):
with open(filename,mode) as fout:
fout.write(contents)
def main():
# my code here
readfile("C:/Users/Johny The Overlord/Documents/Code/test.txt")
if __name__ == "__main__":
main()
url in text1 https://twitter.com/xxx https://twitter.com/yyy https://twitter.com/zzz
text2中的应该有
https://twitter.com/xxx target =&#34; _ blank&#34;&gt; @xxx
https://twitter.com/yyy target =&#34; _ blank&#34;&gt; @yyy
https://twitter.com/zzz 目标=&#34; _blank&#34;&GT; @zzz
但它只写了最后一个网址。 我该如何解决?
答案 0 :(得分:1)
我认为这只是因为写作模式。尝试使用&#34; a&#34;或&#34; +&#34;修改看看https://stackoverflow.com/a/23051095/3759158
答案 1 :(得分:0)
您可以覆盖前一个,尝试在单独的行中编写它(我的意思是写函数)。
在fout.write("\n")
fout.write(contents)
添加到您的写函数中