Reading tab-delimited file with Pandas - works on Windows, but not on Mac

前端 未结 2 858
感情败类
感情败类 2020-12-12 14:05

I\'ve been reading a tab-delimited data file in Windows with Pandas/Python without any problems. The data file contains notes in first three lines and then follows with a he

相关标签:
2条回答
  • 2020-12-12 14:34

    The biggest clue is the rows are all being returned on one line. This indicates line terminators are being ignored or are not present.

    You can specify the line terminator for csv_reader. If you are on a mac the lines created will end with \rrather than the linux standard \n or better still the suspenders and belt approach of windows with \r\n.

    pandas.read_csv(filename, sep='\t', lineterminator='\r')
    

    You could also open all your data using the codecs package. This may increase robustness at the expense of document loading speed.

    import codecs
    
    doc = codecs.open('document','rU','UTF-16') #open for reading with "universal" type set
    
    df = pandas.read_csv(doc, sep='\t')
    
    0 讨论(0)
  • 2020-12-12 14:47

    Another option would be to add engine='python' to the command pandas.read_csv(filename, sep='\t', engine='python')

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