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
  Why can't it extract the data from .txt well? Melcu54 4 1,769 Dec-12-2024, 07:36 PM
Last Post: Melcu54
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,058 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Python script to extract data from API to database melpys 0 855 Aug-12-2024, 05:53 PM
Last Post: melpys
  Extract text from PDF goryee 2 6,341 Jul-08-2024, 06:35 AM
Last Post: Pedroski55
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 3,597 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  What are these python lines for? What are tey doing? Led_Zeppelin 7 2,983 Feb-13-2023, 03:08 PM
Last Post: deanhystad
  extract only text strip byte array Pir8Radio 7 6,971 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  python extract mg24 1 1,628 Nov-02-2022, 06:30 PM
Last Post: Larz60+
  Extract only certain text which are needed Calli 26 12,426 Oct-10-2022, 03:58 PM
Last Post: deanhystad
  counting lines in split data Skaperen 6 2,828 Oct-07-2022, 07:09 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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