Reading a CSV file to pandas works in windows, not in ubuntu

旧巷老猫 提交于 2021-02-10 15:54:57

问题


I have written some scrip in python using windows and want to run it in my raspberry with Ubuntu.

I am reading a csv file with line separator new line. When I load the df I use the following code:

dfaux = pd.read_csv(r'/home/ubuntu/Downloads/data.csv', sep=';')

which loads a df with just one row. I have also tried including the argument lineterminator = '\n\t' which throws this error message:

ValueError: Only length-1 line terminators supported

In windows I see the line breaks in the csv file, whereas when I open it with mousepad in ubuntu I don't see the line breakers, but I see the columns color coded.

How could I read properly the csv?

Thanks!


回答1:


This is almost certainly an issue with the difference in line endings between Windows and... well, everything else. Windows uses a two-character line terminator, "\r\n" (carriage return, followed by newline), whereas Linux and Mac and everything else use just "\n".

Two easy fixes:

  1. Using read_csv(..., engine='python') should remedy the issue. You may also need to specify read_csv(..., lineterminator='\r\n'), but based on the error message you're getting, it looks like it's auto-detecting that anyway. (Function docs)

  2. Fix the file before sending it to Pandas. Something like:

     import io
     csv_data = open(r'/home/ubuntu/Downloads/data.csv').read().replace('\r\n', '\n')
     dfaux = pd.read_csv(io.StringIO(csv_data))
    



回答2:


Well, at then end I solved it by changing the explorer I was using to download the csv file with webdriver from Firefox to Chrome, not sure what´s the reason behind but maybe this will help if you have the same issue in the future



来源:https://stackoverflow.com/questions/63904101/reading-a-csv-file-to-pandas-works-in-windows-not-in-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!