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
Question Special Characters read-write Prisonfeed 1 582 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 787 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,353 Feb-13-2022, 07:13 PM
Last Post: menator01
  Why not getting return on line #16? jahuja73 12 4,287 Feb-12-2021, 02:48 PM
Last Post: deanhystad
  read the first line in my script chubbychub 1 1,698 Nov-09-2020, 03:18 AM
Last Post: Larz60+
  Adding Sliced Positions Gizzmo28 1 1,574 Nov-05-2020, 02:46 AM
Last Post: bowlofred
  Print characters in a single line rather than one at a time hhydration 1 1,992 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 2,030 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,426 May-15-2020, 01:37 PM
Last Post: snippsat
  How to Remove Non-ASCII Characters But Leave Line Breaks In Place? bmccollum 4 4,256 Apr-09-2020, 07:59 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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