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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
  subprocess check_output text cut off Axel_Erfurt 5 757 Feb-20-2025, 02:15 PM
Last Post: DeaD_EyE
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,047 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  Python - Hidden Text / Html Mail python1337 1 2,950 Feb-08-2025, 10:47 AM
Last Post: python1337
  Problems writing a large text file in python Vilius 4 1,004 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,024 Nov-21-2024, 11:48 PM
Last Post: haihal
  Paste text with caret already positioned inside a placeholder; Wehaveall 1 832 Oct-15-2024, 10:28 AM
Last Post: menator01
Photo image generation with text style Belialhun 0 650 Oct-08-2024, 01:53 PM
Last Post: Belialhun
  How to insert text time - into video frames? oxidian 0 1,162 Aug-25-2024, 04:51 PM
Last Post: oxidian
  Print text with big font and style tomtom 6 20,628 Aug-13-2024, 07:26 AM
Last Post: yazistilleriio
  Text Barakaabana 1 733 Aug-10-2024, 08:19 AM
Last Post: Barakaabana

Forum Jump:

User Panel Messages

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