Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

Python Pandas to_csv() blank values problem - from Windows to Mac/Linux

I have created a set of CSVs using Pandas to_csv() method. I am on Windows.

The CSVs are then fed into POST requests that insert the data into a CMS.

However, 2 out of the 15 columns in the CSV are showing up blank after POST request.

My colleague confirms the columns are also blank on her Mac machine.

I've tried optional arguments like encoding='utf-8' and line_terminator=' '.

Related problem: I suspect some optional parameter on to_csv() would do the trick but testing is tricky since I can't replicate the problem locally... I've Ubuntu + Docker and Ubuntu + VirtualBox but as far as I can see the data is showing up OK.

There are a few SO posts similar - but I can't find a good answer for going From_Windows_to_Mac/Linux

# df[df['foo']=='bar'].to_csv('testing-1.csv', encoding='utf-8')
# df[df['foo']=='bar'].to_csv('testing-2.csv', encoding='utf-8', line_terminator='
')


# df[df['foo']=='bar'].to_csv('testing-3.csv', index=False, encoding='ascii')

df[df['foo']=='bar'].to_csv('testing-4.csv', index=False, encoding='ascii', header=False)

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

1 Answer

0 votes
by (71.8m points)

The issue was quotations in CSV b/w Windows and Mac/Linux. Fixed this by adding quoting=csv.QUOTE_NONNUMERIC

import csv
df.to_csv('test.csv', encoding='utf-8', line_terminator='
', quoting=csv.QUOTE_NONNUMERIC)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...