Python Forum
Parsing text list to csv using delimiter discarding non-interesting data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing text list to csv using delimiter discarding non-interesting data
#2
One approach:
read the file in chunks of 20 lines (assuming each separate chunk has the same structure as the two chunks in your example).
you need lines 2 and 12-19 of each chunk.
parse the respective lines (using split, strip, etc.)
write result to your file.

Alternative:
read the file line by line
check if line (stripped of leading spaces) starts with one of the words (LOCATION, color, mandatory, viscosity,winter use, chemicals, safe for kids, accessories, cleaning)
parse the respective line accordingly. Note that you need to write a row to the output file when you find next LOCATION line.

Third approach  - RegEx
This one should be first one, if you are familiar with RegEx
regex that returns all matches from your example [^ \n][\w ]* ?: \w*. There might be a better one, but I'm not that experienced with RegEx

Maybe combine RegEx with reafing chunks of 20 lines/
Reply


Messages In This Thread
RE: Parsing text list to csv using delimiter discarding non-interesting data - by buran - Feb-15-2017, 07:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Text parsing Arik 5 502 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  Help with to check an Input list data with a data read from an external source sacharyya 3 522 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,639 Nov-09-2023, 10:56 AM
Last Post: mg24
  Context-sensitive delimiter ZZTurn 9 1,614 May-16-2023, 07:31 AM
Last Post: Gribouillis
Video doing data treatment on a file import-parsing a variable EmBeck87 15 3,051 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Read csv file with inconsistent delimiter gracenz 2 1,259 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  json api data parsing elvis 0 959 Apr-21-2022, 11:59 PM
Last Post: elvis
  Delimiter issue with a CSV file jehoshua 1 1,362 Apr-19-2022, 01:28 AM
Last Post: jehoshua
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,765 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  I need help parsing through data and creating a database using beautiful soup username369 1 1,746 Sep-22-2021, 08:45 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