Edit Csv Column Using Python
I would like to make column 2 in the csv file to be all lowercase and removing all the punctuation and save the file. How can i do that? import re import csv with open('Cold.csv',
Solution 1:
You could add your code to modify your cell as follows:
import re
import csv
withopen('in.csv', 'rb') as f_input, open('out.csv', 'wb') as f_output:
csv_output = csv.writer(f_output)
forrowin csv.reader(f_input):
row[1] = re.sub('[^A-Za-z0-9]+', '', row[1].lower())
csv_output.writerow(row)
.lower()
is used to first convert the string to lowercase. Using with
ensures that your files are both automatically closed at the end.
Note, your regular expression sub should replace any invalid characters with an empty string, e.g. ''
, you currently have it set to be a single space.
Solution 2:
Just edit the row in place and write it back out
withopen('Cold.csv', 'rb') as f_input1, open('outing.csv', 'wb') as f_output:
reader = csv.reader(f_input1)
writer = csv.writer(f_output)
forrowin reader:
row[1] = re.sub('[^a-z0-9]+', ' ', str(row[1].lower()))
writer.writerow(row)
Solution 3:
Easiest solution would be combining these two approaches in you code.
import string
s.translate(None, string.punctuation) --> to remove punctuation
##if speed is not an issue
exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)
row.lower() --> to convert to lower case
Post a Comment for "Edit Csv Column Using Python"