简体   繁体   中英

parsing tree from csv

How can I simply parsing the tree from file? Actually I am only able to get the 'nodes'

with open('source.txt') as f:
    lines = f.read().splitlines()

for line in lines:
    if line.startswith("+"):
        print(line)

input:

+- Y
|  +- Y1
|  +- Y9
|  +- Y10
|  |  +- Y101
|  |  \- Y81
|  \- Y11
|     +- Y111
|     |  +- Y1111
|     |  \- Y1112
|     +- Y112
|     +- Y113
|        \- Y1131
+- Z
|  +- Z1
|  |  +- Z11
|  |  +- Z14
|  |  \- Z15
|  +- Z2
|  |  \- Z21
|  |     \- Z211
|  |        +- Z2111
|  |        \- Z2112
|  \- Z3

output should be:

X,Y,Y1;Y9;Y10;Y101;Y81;Y11;Y111;Y1111;Y1112;Y112;Y113;Y1131

X,Z,Z1;Z11;Z14;Z15;Z2;Z21;Z211;Z2111;Z2112;Z3

Input:

It's not parsing actually. You are just rewriting names in each line. This code may be helpful:

import re

print('X,Y,' + ';'.join(re.findall(r'Y.+',s)))
print('X,Z,' + ';'.join(re.findall(r'Z.+',s)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM