Adding a line-terminator in pandas ends up adding another \r

后端 未结 2 1182
故里飘歌
故里飘歌 2020-12-21 23:56

I am able to load a csv file fine into a pandas dataframe with the panda defaults:

df = pd.read_csv(file)

>>> df
   distance  recession_velocity
0          


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-21 23:58

    Python's file objects will automatically translate \r\n to \n in text mode. read_csv uses its own file handling, it will indeed see \r\n instead, so if you pass lineterminator="\n" it will really just trim that one character.

    If you don't pass the lineterminator parameter at all, it will guess the line-ending style. You can also pass in a file object instead of a path. This may slow things down a bit, but it will give you the same transformation behaviour that you see when you do a straight read.

提交回复
热议问题