python opens text file with a space between every character

前端 未结 8 1620
野性不改
野性不改 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:35

    To read an encoded file, you can simply replace open with codecs.open.

    fread = codecs.open('input.csv', 'r', 'utf-16')
    
    0 讨论(0)
  • 2021-02-18 17:40

    The file is encoded in some unicode encoding, but you are reading it as ascii. Try to convert the file to ascii before using it in python.

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

    Ok, I got it with the help of Jarret Hardie's post

    this is the code that I used to convert the file to ascii

    fread = open('input.csv', 'rb').read()
    mytext = fread.decode('utf-16')
    mytext = mytext.encode('ascii', 'ignore')
    fwrite = open('input-ascii.csv', 'wb')
    fwrite.write(mytext)
    

    Thanks!

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

    Isn't csv a simple txt file with values separated with comma. Just try to open it with a text editor to see if the file is correctly formed.

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

    Open the file in binary mode, 'rb'. Check it in a HEX Editor and check for null padding '00'. Open the file in something like Scintilla Text Editor to check the characters present in the file.

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

    It did never ocurred to me, but as truppo said, it must be something wrong with the file.

    Try to open the file in Excel/BrOffice Calc and Save As the file as Csv again.

    If the problem persists, try a subset of the data: fist 10/last 10/intermediate 10 lines of the file.

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