Python Forum
parsing a tree of text first the right most aligned blocks of text and so on
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parsing a tree of text first the right most aligned blocks of text and so on
#1
Hi

I have a tree / table of contents

Cover	  0
0    	Copyright	  0
0    	Credits	  0
0    	About the Authors	  0
0    	About the Reviewer	  0
0    	Link Removed	  0
0    	Table of Contents	  0
0    	Preface	  0
1    	Chapter 1: Current Status of Python	  1
2    		Why and how does Python change?	  2
2    		Why and how does Python change?	  2
3    		Getting up to date with changes – PEP documents	  3
4    		Python 3 adoption at the time of writing this book	  4
5    		The main syntax differences and common pitfalls	  5
5    			The main syntax differences and common pitfalls	  5
5    			The main syntax differences and common pitfalls	  5
6    				Syntax changes	  6
7    				Changes in the standard library	  7
8    				The popular tools and techniques used for maintaining cross-version compatibility	  8
8    			The popular tools and techniques used for maintaining cross-version compatibility	  8
12   		Not only CPython	 12
13   			Stackless Python	 13
13   			Stackless Python	 13
14   			IronPython	 14
14   			IronPython	 14
15   			PyPy	 15
16   		Modern approaches to Python development	 16
17   		Application-level isolation of Python environments	 17
19   			Why isolation?	 19
21   			virtualenv	 21
21   				virtualenv	 21
23   				venv	 23
24   				Which one to choose?	 24
24   			Which one to choose?	 24
25   		System-level environment isolation	 25
26   			Virtual development environments using Vagrant	 26
27   			Containerization versus virtualization	 27
28   		Popular productivity tools	 28
29   			Custom Python shells – IPython, bpython, ptpython, and so on	 29
30   				bpython	 30
30   				bpython	 30
30   				bpython	 30
31   				Interactive debuggers
Now first I have to parse those lines of text that are right most aligned, then the next right most aligned and so on.
By right most aligned, I mean the first letter of the first word's starting position 's right alignment relative to pos = 1 cursor from left.

How do I go about doing this?

Thanks
Arvind.
Gribouillis write Nov-18-2024, 12:30 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Also I removed irrelevant link
Reply
#2
Which output do you expect from the parsing of this text?
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
This will give you the number of spaces before each line. I hope this helps Smile

raw_text = """Cover     0
0       Copyright     0
0       Credits   0
0       About the Authors     0
0       About the Reviewer    0
0       Link Removed      0
0       Table of Contents     0
0       Preface   0
1       Chapter 1: Current Status of Python   1
2           Why and how does Python change?   2
2           Why and how does Python change?   2
3           Getting up to date with changes – PEP documents   3
4           Python 3 adoption at the time of writing this book    4
5           The main syntax differences and common pitfalls   5
5               The main syntax differences and common pitfalls   5
5               The main syntax differences and common pitfalls   5
6                   Syntax changes    6
7                   Changes in the standard library   7
8                   The popular tools and techniques used for maintaining cross-version compatibility     8
8               The popular tools and techniques used for maintaining cross-version compatibility     8
12          Not only CPython     12
13              Stackless Python     13
13              Stackless Python     13
14              IronPython   14
14              IronPython   14
15              PyPy     15
16          Modern approaches to Python development  16
17          Application-level isolation of Python environments   17
19              Why isolation?   19
21              virtualenv   21
21                  virtualenv   21
23                  venv     23
24                  Which one to choose?     24
24              Which one to choose?     24
25          System-level environment isolation   25
26              Virtual development environments using Vagrant   26
27              Containerization versus virtualization   27
28          Popular productivity tools   28
29              Custom Python shells – IPython, bpython, ptpython, and so on     29
30                  bpython  30
30                  bpython  30
30                  bpython  30
31                  Interactive debuggers
"""

text_lines = raw_text.splitlines ()
for line in text_lines :
	# determine the number of spaces between first numbers and first letter
	new_line = line [2: -1]   # remove leading numbers
	indention = len (new_line) - len (new_line.lstrip ())
	print ("\n", line)
	print (f"     Indenttion is {indention}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  open a text file using list() Pedroski55 2 115 Feb-25-2026, 06:57 PM
Last Post: noisefloor
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 7,273 Dec-16-2025, 05:29 PM
Last Post: zodazy
  link variable to exc blocks garbage collection Astrobert 1 747 Nov-19-2025, 05:03 AM
Last Post: Gribouillis
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
Question Parse Markdown / get the plain text SpongeB0B 8 4,290 Oct-07-2025, 06:14 PM
Last Post: noisefloor
  replace or remove text from many text files Curbie 21 3,245 Jul-07-2025, 02:05 PM
Last Post: Curbie
  Picamera2 add text to camera preview GigiG 0 992 May-26-2025, 11:46 AM
Last Post: GigiG
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 36,670 May-20-2025, 12:26 PM
Last Post: hanmen9527
  Paste text with caret already positioned inside a placeholder; Wehaveall 2 1,704 May-14-2025, 01:12 AM
Last Post: armorerratic
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 1,455 Apr-24-2025, 01:05 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