Python Forum
Read characters of line and return positions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read characters of line and return positions
#1
Hello, I am trying to pull out certain information from a huge text file because the lines are difficult to read. I can read line by line but how do I put each character in a list or array to pull out characters at certain positions? Do I use split or strip? Here is an example of a line:

0000009887465 000002250000000000089877000000000000.0002/28/202000000000.00000000000.00

I need to pull position 6 thru 13 and to get: 9887465
Then pull 27 thru 30 and 61 thru 71 to get: 225 02/28/2020

So the output would be 9887465 225 02/28/2020

If someone could point me in the right direction. Thanks!

ps. there is supposed to 9 white spaces after 65
Reply
#2
Slice
long_str = '0000009887465 000002250000000000089877000000000000.0002/28/202000000000.00000000000.00'
print(long_str[6:13])
print(long_str[19:22])
print(long_str[53:63])
Output:
9887465 225 02/28/2020
Gizzmo28 likes this post
Reply
#3
Iterating on deanhystad solution. It's more lines of code but presumably if you return to it in couple of weeks this makes much easier to remember what it's all about.

data = "0000009887465 000002250000000000089877000000000000.0002/28/202000000000.00000000000.00"

first_ID = slice(6, 13)
second_ID = slice(19, 22)
date = slice(53, 63)

extracted = f'{data[first_ID]} {data[second_ID]} {data[date]}'
print(extracted) -> 9887465 225 02/28/2020
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why not getting return on line #16? jahuja73 12 1,147 Feb-12-2021, 02:48 PM
Last Post: deanhystad
  read the first line in my script chubbychub 1 494 Nov-09-2020, 03:18 AM
Last Post: Larz60+
  Adding Sliced Positions Gizzmo28 1 551 Nov-05-2020, 02:46 AM
Last Post: bowlofred
  Print characters in a single line rather than one at a time hhydration 1 701 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 688 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Remove escape characters / Unicode characters from string DreamingInsanity 5 3,806 May-15-2020, 01:37 PM
Last Post: snippsat
  Read a CSV file only from the 4th line/row asaforen7 8 1,779 Apr-16-2020, 01:49 AM
Last Post: Larz60+
  How to Remove Non-ASCII Characters But Leave Line Breaks In Place? bmccollum 4 1,538 Apr-09-2020, 07:59 PM
Last Post: DeaD_EyE
  Read all csv files, and store the last line from each folder SriRajesh 4 3,527 Jan-17-2020, 12:13 AM
Last Post: SriRajesh
  How to read text file line by line SriRajesh 1 824 Nov-05-2019, 01:51 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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