python opens text file with a space between every character

前端 未结 8 1621
野性不改
野性不改 2021-02-18 17:27

Whenever I try to open a .csv file with the python command fread = open(\'input.csv\', \'r\') it always opens the file with spaces between every single character.

相关标签:
8条回答
  • 2021-02-18 17:58

    The post by recursive is probably right... the contents of the file are likely encoded with a multi-byte charset. If this is, in fact, the case you can likely read the file in python itself without having to convert it first outside of python.

    Try something like:

    fread = open('input.csv', 'rb').read()
    mytext = fread.decode('utf-16')
    

    The 'b' flag ensures the file is read as binary data. You'll need to know (or guess) the original encoding... in this example, I've used utf-16, but YMMV. This will convert the file to unicode. If you truly have a file with multi-byte chars, I don't recommend converting it to ascii as you may end up losing a lot of the characters in the process.

    EDIT: Thanks for uploading the file. There are two bytes at the front of the file which indicates that it does, indeed, use a wide charset. If you're curious, open the file in a hex editor as some have suggested... you'll see something in the text version like 'I.D.|.' (etc). The dot is the extra byte for each char.

    The code snippet above seems to work on my machine with that file.

    0 讨论(0)
  • 2021-02-18 17:58

    Here's the quick and easy way, esp if python won't parse the input correctly

    sed 's/ \(.\)/\1/g'
    
    0 讨论(0)
提交回复
热议问题