Making a new CSV file after using Pandas to eliminate whitespace in original CSV file

Question:

I have a CSV file that has whitespace in the information and I’ve used Pandas to read the data without whitespace:

import csv
import pandas as cr

csv_file = cr.read_csv("forest-fire-data.csv', header=0)
print(csv_file)

The code does print out the data from the csv without whitespace but now I need to convert that to a new csv file so that it has no whitespaces.

So far, I thought this would work but it just creates a new csv with the info from the old csv and the whitespaces still show up:

new_csv_file = csv_file.to_csv('new-forest-fire-data.csv')

I’d appreciate any help. Thank you.

EDIT:

The CSV File I have looks similar to this:

firedepartmentresponse  |  fire_year  |  Point_X  |  Point_Y
     YES                      2010          -79.1       35.9
     NO                       2011          -79.2       35.7
     YES                      2011          -80.1       35.0

The CVS File I’m trying to get should get rid of the whitespace that the data has leading on it.

firedepartmentresponse|fire_year|Point_X|Point_Y
YES                    2010      -79.1   35.9
NO                     2011      -79.2   35.7
YES                    2011      -80.1   35.0
Asked By: MappingWAVE

||

Answers:

Your question is not totally clear to me. I tried to run your original code imagining this starting csv file:

policyID, state code, county, construction, point_granularity
119736, FL, CLAY COUNTY, Masonry Space, 1
448094, FL, CLAY COUNTY, Masonry, 3

I modified the original state_code into state code and Masonry into Masonry Space. With your original working code I have these two spaces printed

So I thought that you may be searching a way to delete whitespaces after the separator. In this case you can run this:

import csv
import pandas as cr

#csv_file = cr.read_csv('forest-fire-data.csv')
csv_file = cr.read_csv('forest-fire-data.csv', skipinitialspace=True, delimiter=',', quoting=csv.QUOTE_NONE)
print(csv_file)

new_csv_file = csv_file.to_csv('new-forest-fire-data.csv', index=False)

and the output for new-forest-fire-data.csv would be:

policyID,state code,county,construction,point_granularity
0,119736,FL,CLAY COUNTY,Masonry Space,1
1,448094,FL,CLAY COUNTY,Masonry,3

NOTE: index=False is needed to remove the initial comma from the output

Answered By: Antonino
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.