Python Forum

Full Version: how to give your data column names based on char position
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All

I am very new to python .

I am working with a file with no column header. But the task is to give the file column names based on char positions/substring of data.i have gotten so far to get the first column as below . But i do not know how to actually process the file so it will have column names by the end of it. Please help. Thank you!


<<code>>
with open("/raw10.txt","rb") as f:
... for line in f:
... rec_bytes = int(line[:3])

<<code>>
Can you print some lines of raw10.txt here ?
Hi
Thanks for replying . But for security reasons , i cannot post data.

But it may be something like :

173DonaldDuckTrentonNJ102717
185DavidBeckhamMorrisvillePA102717
193JenniferLawrencePhoenixAZ102717
201JuliaRobertsNewYorkNY102717
This is difficult to parse without any delimiter.
Ok . Thanks for replying.

I was hoping i could use substrings
like line[:3] = length
line[4:9] = first_name ...something like that . Isn't that possible ? This is just an example . do not worry about the first_name etc having variable lengths in the sample i posted.
But Firstname does not have always 5 characters.
So you have to parse for upper and lower chars.
yes ,but what i printed is dummy data of my own . The actual data always has a fixed length for the different values . In that case , do you have any suggestions how to go about it? Thanks for your time!
Then show us a better sample of raw.txt