Python 2.7 - find and replace from text file, using dictionary, to new text file

后端 未结 3 849
别跟我提以往
别跟我提以往 2021-01-05 17:27

I am newbie to programming, and have been studying python in my spare time for the past few months. I decided I was going to try and create a little script that converts Ame

3条回答
  •  伪装坚强ぢ
    2021-01-05 18:01

    The print statement adds a newline of its own, but your lines already have their own newlines. You can either strip the newline from your new_line, or use the lower-level

    output.write(new_line)
    

    instead (which writes exactly what you pass to it).

    For your second question, I think we need an actual example. replace() should indeed replace all occurrences.

    >>> "abc abc abcd ab".replace("abc", "def")
    'def def defd ab'
    

    I'm not sure what your third question is asking. If you want to replace the output file, do

    output = open('output_test_file.txt', 'w')
    

    'w' means you're opening the file for writing.

提交回复
热议问题