Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 views
in Technique[技术] by (71.8m points)

python - Adding a line-terminator in pandas ends up adding another

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   # not a row                 NaN
1         0.032               170.0
2         0.034               290.0
3         0.214              -130.0

However, as soon as I add the lineterminator, the program seems to go haywire:

df = pd.read_csv(file, lineterminator='
')
       distance recession_velocity

0   # not a row                   

1         0.032                170

2         0.034                290

3         0.214               -130

The file indeed does have a line separator:

>>> print(repr(open('/Users/david/example.csv').read()))
'distance,recession_velocity
# not a row,
0.032,170
0.034,290
0.214,-130
0.263,

What is the issue here and is there a way to fix it without having to trim all the column values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Python's file objects will automatically translate to in text mode. read_csv uses its own file handling, it will indeed see instead, so if you pass lineterminator=" " 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...