Reputation: 1376
I want to fix my CSV file's case issues as simply as possible. I already have pandas imported if that simplifies the code.
So I want to replace
Name City State
FOO BAR los angeles ca
guy's naME PHILADELPHIA Pa
With (note the ')
Name City State
Foo Bar Los Angeles CA
Guy's Name Philadelpha PA
Upvotes: 2
Views: 1083
Reputation: 227
This is hard-coded to match your example file but should do what you want:
import csv
import sys
import string
reader = csv.reader(sys.stdin, delimiter='\t')
writer = csv.writer(sys.stdout, delimiter='\t')
# write the header as-is
writer.writerow(reader.next())
for row in reader:
row[0] = string.capwords(row[0])
row[1] = string.capwords(row[1])
row[2] = row[2].upper()
writer.writerow(row)
Example usage:
cat test.csv | python fix_case.py
Name City State
Foo Bar Los Angeles CA
Guy's Name Philadelphia PA
Upvotes: 3
Reputation: 767
This will do what you want minus the all capital states and cases like (Guys'S Name) with special characters:
with open("output.txt",'w') as O:
with open("input.txt") as I:
for line in I:
O.write(line.title())
Before:
Name City State
FOO BAR los angeles ca
guy's naME PHILADELPHIA Pa
After:
Name City State
Foo Bar Los Angeles Ca
Guy'S Name Philadelphia Pa
Upvotes: 2
Reputation: 1032
First of all, you need to learn some of python rules Then, you have to install ipython, and use it instead of python. Then, you have to run ipython, and try some really basic problems.
Read csv file to DataFrame object(google it, or learn) Then, if you want to UPPERCASE Series(State):
s1 = pd.core.series.Series(['foo', 'bar'])
s1 = s1.apply(lambda x: x.upper())
s1
Then if you want to lowercase and capitalize Series(City)
s2 = pd.core.series.Series(['FOO', 'fOo'])
s2 = s2.apply(lambda x: x.lower().capitalize())
s2
The if you want to capitalize each word, you have to google for it.
And then you have to save you csv file with you new changed DataFrame.
Upvotes: 0