I realize that there is a way to add a column using 'awk'.
But I'm not so familiar with this alternative, so I though I'd ask whether there's a way to add a column to a tab-delimited text file using Python?
Specifically, here's the scenario I need to add a column in:
I have data in the following format (I realize looking at it that the format may not be so clear, but the phone, email, and website correspond to different columns):
name phone email website
D G Albright M.S.
Lannister G. Cersei M.A.T., CEP 111-222-3333 cersei@got.com www.got.com
Argle D. Bargle Ed.M.
Sam D. Man Ed.M. 000-000-1111 dman123@gmail.com www.daManWithThePlan.com
Sam D. Man Ed.M.
Sam D. Man Ed.M. 111-222-333 dman123@gmail.com www.daManWithThePlan.com
D G Bamf M.S.
Amy Tramy Lamy Ph.D.
And I'm writing a parser for the first column. I want to add the 'area of practice', in this case an ex would be 'CEP', to a new column entitled 'area'. I iterate through the file, and use the pop function to separate out the area from the rest of the first column. Then I add this to a list, which just dies in the function because it's not added to the spreadsheet.
Here's my script:
def parse_ieca_gc(s):
### HANDLE NAME ELEMENT ######
degrees = ['M.A.T.','Ph.D.','MA','J.D.',
'Ed.M.', 'M.A.', 'M.B.A.',
'Ed.S.', 'M.Div.', 'M.Ed.',
'RN', 'B.S.Ed.', 'M.D.', 'M.S.']
degrees_list = []
# check whether the name string has
# an area of practice by
# checking if there's a comma separator
if ',' in s['name']:
# separate area of practice from name
# and degree and bind this to var 'area'
split_area_nmdeg = s['name'].split(',')
area = split_area_nmdeg.pop()
# Split the name and deg by spaces.
# If there's a deg, it will match with one
# of elements and will be stored deg list.
# The deg is removed name_deg list
# and all that's left is the name.
split_name_deg = re.split('\s',split_area_nmdeg[0])
for word in split_name_deg:
for deg in degrees:
if deg == word:
degrees_list.append(split_name_deg.pop())
name = ' '.join(split_name_deg)
Expected output
name phone email website area degrees
D G Albright M.A.
Lannister G. Cersei 111-222-3333 cersei@got.com www.got.com CEP M.A.T.
Argle D. Bargle Ed.M.
Sam D. Man 000-000-1111 dman123@gmail.com www.daManWithThePlan.com Ed.M.
Sam D. Man Ed.M.
Sam D. Man 111-222-333 dman123@gmail.com www.daManWithThePlan.com Ed.M.
D G Bamf M.S.
Amy Tramy Lamy Ph.D.
This code is also not working:
fieldnames = ['name','degrees','area','phone','email','website']
with open('ieca_first_col_fake_text.txt','r') as input:
with open('new_col_dict.txt','w') as output:
dict_writer = csv.DictWriter(output, fieldnames, delimiter = '\t')
dict_reader = csv.DictReader(input, delimiter = '\t')
#dict_writer.writeheader(fieldnames)
for row in dict_reader:
print row
dict_writer.writerow(fieldnames)
dict_writer.writerow(row)