Python: Unicode source file adds spaces (actually null bytes) between characters

后端 未结 1 977
醉梦人生
醉梦人生 2021-01-12 10:20

I am a newbie. However, I managed to extract some lines from a txt-file (unicode) and write them in another file.

lines = InFile.readlines()
OutFile.writeli         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 10:31

    I'm fairly certain that your input file is UTF-16 encoded, and the spaces you're seeing are actually null bytes.

    Try

    with open("myfile.txt", "r", encoding="utf-16") as infile:
        lines = infile.readlines()
    

    and see if the problem persists.

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