I am trying to tie another piece into my existing Python program. I am new to Python and can't seem to figure it out, even with all of the help out there. I will list my existing Python program below, I would just like to add in the other piece to perform another task.
The current program opens "initial.csv" and looks in the first column for any key words. If it matches one, it writes the line in "listname_rejects.csv" and any that don't match, it writes into "listname.csv". It sounds backwards, but for what I'm doing, it's correct. I've used it a thousand times.
Now, what I would like to add into this, is the ability to look at column 2 (Full of addresses) and split them up into separate columns. For example, this -
Name,Address,Phonenumber,ID
John,"123 Any Street, New York, NY 00010",999-999-9999,321654
Turns into this -
Name,Street,City,State,Zipcode,Phonenumber,ID
John,123 Any Street, New York, NY, 00010,999-999-9999,321654
Basically, I need to be able to explode the second column into separate columns. Rather than having the entire address in column 2, I need to split it up between say column 2, 3, 4, & 5.
I have found things close to this on stack overflow, but again, I'm new to Python and can't figure out how to piece them into my current code.
key_words = [
'Suzy',
'Billy',
'Cody',
]
listname = raw_input ("Enter List Name:")
listname_accept = (listname) + '.csv'
listname_rejects = (listname) + '_rejected.csv'
with open('initial.csv', 'r') as oldfile, open(listname_accept, 'w') as cleaned:
for line in oldfile:
if not any(key_word in line.split(",", 1)[0] for key_word in key_words):
cleaned.write(line)
else:
matched.write(line)
csvmodule. It is easier and avoids many bugs. Second: parsing addresses is far from trivial if you want to handle addresses correctly. Do some searching to find some of the various crazy ways valid streets and address numbers occur in the US.addressfollows this format:"<street>,<city>,<state zip>"?