我想从text1读取url并将edit url写入text2

时间:2018-01-14 15:09:08

标签: python-3.x

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
  • 但它只写了最后一个网址。 我该如何解决?

    2 个答案:

    答案 0 :(得分:1)

    我认为这只是因为写作模式。尝试使用&#34; a&#34;或&#34; +&#34;修改看看https://stackoverflow.com/a/23051095/3759158

    答案 1 :(得分:0)

    您可以覆盖前一个,尝试在单独的行中编写它(我的意思是写函数)。

    fout.write("\n")

    之后将fout.write(contents)添加到您的写函数中