Python Forum
how to extract a portion of data from text lines by python 2
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to extract a portion of data from text lines by python 2
#1



I have a text file including a string header and data body. The contents seem like this:

this is a technical data file....................COMMENT
the creator : Adams............................COMMENT
2017.05.10.......................................THE FIRST TIME
.........................................................END OF HEADER
OA 10123.4532 12345.0102 -1827734.3475 -1893255.1023 45.12 23.01 8923.12
XB 10125.4132 13345.0702 -1843734.7875 -1834255.1913 44.12 23.02 8924.12
...
...
.........................................................END OF FILE

I need to write some codes with python 2 to extract only portions of data from the lines and get like:

10123.45 12345.01 -1827734.34 -1893255.10
10125.41 13345.07 -1843734.78 -1834255.19
...

How can I get them? Please give some suggestions. Thanks

BTW, I thought to use the ‘read’ with formmated strings, but I found no resources online about that use. I just found some modules like struct. But is there any concise method to finish that? After all the more modules could decrease the efficiency of the program.
I tried the following codes to read the data lines but not succeeded:
with open('example.txt', 'r') as f:
... line = f.readline('%2s%11.2f%11.2f%14.2f%%14.2f')
....

BTW, all the commas is actually the blank space, 'cause I do not know why the space was canceled automatically for the header lines.
Reply
#2
write out each line without formatting, and then do your formatting when the data is read back in
This can be done in a module that is called by any program that needs the data.

A better solution, is to format the data beforehand in a structure, like a list or dictionary that
can be saved in json format using json.dump

Then when you read it back in with json.load it will already be formated.
Reply
#3
Do all lines you are interested in starting with 'OA' or 'XB'? I'd read each line and check if starts with one of these. Split the line to get it as a list. Slice it from 1 to 5. Then using list comprehension create e new list as rid of the last two symbols of each number. Finally, join it. The for loop will get four lines of code and you are done.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 945 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Why can't it extract the data from .txt well? Melcu54 3 646 Aug-20-2023, 10:07 PM
Last Post: deanhystad
  What are these python lines for? What are tey doing? Led_Zeppelin 7 1,561 Feb-13-2023, 03:08 PM
Last Post: deanhystad
  extract only text strip byte array Pir8Radio 7 2,790 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  python extract mg24 1 918 Nov-02-2022, 06:30 PM
Last Post: Larz60+
  Extract only certain text which are needed Calli 26 5,615 Oct-10-2022, 03:58 PM
Last Post: deanhystad
  counting lines in split data Skaperen 6 1,350 Oct-07-2022, 07:09 PM
Last Post: Skaperen
  python Extract sql data by combining below code. mg24 1 915 Oct-03-2022, 10:25 AM
Last Post: mg24
  SQL Alchemy help to extract sql data into csv files mg24 1 1,679 Sep-30-2022, 04:43 PM
Last Post: Larz60+
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,226 Jul-27-2022, 12:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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