问题
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:
Using
read_csv(..., engine='python')
should remedy the issue. You may also need to specifyread_csv(..., lineterminator='\r\n')
, but based on the error message you're getting, it looks like it's auto-detecting that anyway. (Function docs)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