How to delete non-ASCII characters in a text file?

前端 未结 1 1303
说谎
说谎 2020-12-22 09:32

I have this .log file that I changed the extension name into .txt file but it still reads as log file

but after I copied it and paste it a new editor and sa

相关标签:
1条回答
  • 2020-12-22 10:25

    In Python you can specify the input encoding.

    with open('trendx.log', 'r', encoding='utf-16le') as reader, \
         open('trendx.txt', 'w') as writer:
       for line in reader:
            if "ROW" in line:
               writer.write(line)
    

    I have obviously copied over some stuff from your earlier questions. Kudos for finally identifying the actual problem.

    Notice in particular how we avoid reading the entire file into memory and instead processing a line at a time.

    0 讨论(0)
提交回复
热议问题