goldisfine goldisfine - 4 months ago 46
Python Question

How to insert headers into a tab delimited file?

So I have the following datasource ("/" represents tab delimited locales), and I want to get it into a JSON format. The data have no headers, and I'd like to be able to insert one for the name, the degree, the area (CEP), the phone number, the email, and the url. Not sure if this will be possible for the first column which contains multiple variables.

Any recommendations on how to insert headers and then parse the first column? The csv module has the "has_header" function, but I want to insert a header.

Rxxxx G. Axxxx M.A.T., xxx 561-7x0-xxx
Pxxxx D. Axxxx Ed.M.
xxxxx D. xxxx Ed.M. 413-xxx-xxxx
xxxxx xxxx xxxxx M.S.
xxx xxx xxxxxx M.S.
xxxxxx R. xxxxx M.B.A.
xxxxxx xxxxxx M.A.Ed., CEP

This is a similar post

And I've tried:

echo $'name\ phone\ email\ url' | cat - IECA_supplement_output.txt > ieca_supp_output.txt

but this doesn't work. It merely gives me 'name\ phone...' at the tope and then the data right beneath. The header is not separated by tab.s


First make sure that delimiter is really what you think. You can check this by opening the file using openoffice or writing a python function to detect the delimiter using regular expressions (re module). Also make sure that lines are ending with "/n" or windows style (additional r).

Header is nothing more than the first line. So open the file in python, read all lines. Prepend the header string (separated by /t for tab delimiter ) to the first line. Write the lines back to the file. That's it.