I am new to python and just need a small help.
We have a Pipe delimited CSV file which looks like this
DATE|20160101
ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | A... | 210 W.. | Mo.. | AL... | '31.. | 334.. | '01023 | 10063 |
20 | B... | 240 N.. | Ne.. | Ut... | '21.. | 335.. | '01024 | 10064 |
Every value of Zip and OPEID columns has apostrophes in the beginning
So we wish to create a new CSV file where apostrophes are removed from each value of these 2 columns.
The new file should then look like this:
DATE|20160101
ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | A... | 210 W.. | Mo.. | AL... | 31.. | 334.. | 01023 | 10063 |
20 | B... | 240 N.. | Ne.. | Ut... | 21.. | 335.. | 01024 | 10064 |
This code works for copying data without removing apostrophes
import os
import csv
file1 = "D:\CSV\File1.csv"
with open(file1, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter = '|')
path = "D:/CSV/New"
if not os.path.exists(path):
os.makedirs(path)
writer = csv.writer(open(path+"File2"+".csv", 'wb'), delimiter = '|')
for row in reader:
writer.writerow(row)
csvfile.close()