Python Forum
how to give your data column names based on char position
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to give your data column names based on char position
#1
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>>
Reply
#2
Can you print some lines of raw10.txt here ?
Reply
#3
Hi
Thanks for replying . But for security reasons , i cannot post data.

But it may be something like :

173DonaldDuckTrentonNJ102717
185DavidBeckhamMorrisvillePA102717
193JenniferLawrencePhoenixAZ102717
201JuliaRobertsNewYorkNY102717
Reply
#4
This is difficult to parse without any delimiter.
Reply
#5
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.
Reply
#6
But Firstname does not have always 5 characters.
So you have to parse for upper and lower chars.
Reply
#7
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!
Reply
#8
Then show us a better sample of raw.txt
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning Column and Row Data From Spreadsheet knight2000 0 453 Oct-22-2023, 07:07 AM
Last Post: knight2000
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,162 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Database that can compress a column, or all data, automatically? Calab 3 1,197 May-22-2023, 03:25 AM
Last Post: Calab
  Code for pullng all data in a column EmBeck87 5 1,124 Apr-03-2023, 03:43 PM
Last Post: deanhystad
  Grouping Data based on 30% bracket purnima1 4 1,203 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  create new column based on condition arvin 12 2,262 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 850 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  conditionals based on data frame mbrown009 1 906 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  I have written a program that outputs data based on GPS signal kalle 1 1,185 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
Question Change elements of array based on position of input data Cola_Reb 6 2,143 May-13-2022, 12:57 PM
Last Post: Cola_Reb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020