Hi, there

Thank you so much for reading my question. I have apart of a text file with a structure as following:

LOAD
NAME=LIVE
TYPE=UNIFORM
ADD=110 UZ=-4500
ADD=113 UZ=-4500
ADD=114 UZ=-4500
ADD=120 UZ=-4500
ADD=121 UZ=-4500
NAME=SIDL
TYPE=UNIFORM
ADD=110 UZ=-850
ADD=113 UZ=-850

NAME=CLADDING
TYPE=DISTRIBUTED SPAN
ADD=15470 RD=0,1 UZ=-4500,-4500
ADD=15471 RD=0,1 UZ=-4500,-4500
ADD=15472 RD=0,1 UZ=-4500,-4500

MODE

My target is
1) locate the lines with "LOAD", "NAME= LIVE","NAME=SIDL" and "NAME=CLADDING". In other words, how to get their line number?
2) use obtained line numbers to define sevearl search regions by using While loop and then take other actions. For example, perform re.sub between the line starting with "NAME=SIDL" and the line starting with "NAME=CLADDING".

Could you please show me how to achieve Step1. Thank you so much.

best wishes

ning

You could read everything in as a single string using read() and then use find() on the resulting string to determine the locations of the substrings that you are looking for. Once you know the locations you can easily do editing on any text in between. I don't see why you would necessarily need to know the line numbers in order to accomplish this task. You could read it in line by line and test each line to find the line numbers or you could split the read in string by end line characters and use index() on the resulting list if you can not live without knowing the line numbers.

Comments
He help me a lot

Hi, Nine Tails

Thank you so much again. If you look at the data part below:
NAME=LIVE
TYPE=UNIFORM
ADD=110 UZ=-4500
ADD=113 UZ=-4500
ADD=114 UZ=-4500
ADD=120 UZ=-4500
ADD=121 UZ=-4500

NAME=SIDL
TYPE=UNIFORM
ADD=110 UZ=-850
ADD=113 UZ=-850

and will realise that the part
NAME=LIVE
TYPE=UNIFORM
ADD=110 UZ=-4500
ADD=113 UZ=-4500
ADD=114 UZ=-4500
ADD=120 UZ=-4500
ADD=121 UZ=-4500

has the same structure with the part
NAME=SIDL
TYPE=UNIFORM
ADD=110 UZ=-850
ADD=113 UZ=-850

I have to distinguish them to perform different math calculation later on. In other words, I have to separte the part with "NAME=LIVE" from "NAME=SIDL".

I think index() might help me out, as you suggested above.

all the best

ning

Hi,

I used a stupid code below
i=0
for line99 in alllines:
i=i+1
if "LOAD" in line99:
print "Line number is", i
break

to achieve line number.

Any better code? Thanks.

ning

Hi, guys

Another update about my question.

I used the stupid code mentioned above to identify line numbers as
113961 and 114961.

How can I use these two line numbers to specify a While or For loop? I am just jumping from a hole into another one. haha.

Ning

Hi,

I used a stupid code below

i=0
for line99 in alllines:
    i=i+1
    if "LOAD" in line99:
        print "Line number is", i
        break

to achieve line number.

Any better code? Thanks.

ning

You can do (here, i imagine that you did something like alllines = a_file.readlines() ):

for i, line99 in enumerate(file("/path/to/infile")):
    if "LOAD" in line99:
        print "Line number is", i+1
        break

Hi, guys

Another update about my question.

I used the stupid code mentioned above to identify line numbers as
113961 and 114961.

How can I use these two line numbers to specify a While or For loop? I am just jumping from a hole into another one. haha.

Ning

Why do you want to reloop on some part of you file instead of process it the first time.

Jice,

Thank your code again.

Actaully I just do not how to extract data I wanted. I need a starting index and a stop index to avoid extracting wrong data.
Yes, you are right. Why do I reloop? Why? I am lost. My question should be refined as that

How to extract lines between NAME=LIVE and NAME=SIDL, NAME=SIDL and NAME=CLADDING? They have the same structure. So I have to find a way to specify their boundaries. But How?

I have not programming for a while and cannot generate a clear clue at present.

Ning

If you want a really simple solution you can loop through and find the line numbers of each of the tags that you are looking for. You then know that the data you are looking for occurs on line numbers in between the tag line numbers.

If you want a really simple solution you can loop through and find the line numbers of each of the tags that you are looking for. You then know that the data you are looking for occurs on line numbers in between the tag line numbers.

Hi, Nine Tails,

I just sorted it out using the stupid line number. Hopefully I will get a smart way to handle it. Thank you again.

ning

Here is a way to process you file.
This is just a quick example that you'll have to adapt.
The idea is to store the section of the file you're in and then adapt the line processing depending on the section.

datas = """
NAME=LIVE
TYPE=UNIFORM
ADD=110 UZ=-4500
ADD=113 UZ=-4500
ADD=114 UZ=-4500
ADD=120 UZ=-4500
ADD=121 UZ=-4500

NAME=SIDL
TYPE=UNIFORM
ADD=110 UZ=-850
ADD=113 UZ=-850

and will realise that the part
NAME=LIVE
TYPE=UNIFORM
ADD=110 UZ=-4500
ADD=113 UZ=-4500
ADD=114 UZ=-4500
ADD=120 UZ=-4500
ADD=121 UZ=-4500
"""
section='' # which part of the file are we in
for line in datas.split('\n'): # depends on the way datas are presented maybe "from line in file("/path/to/file.txt"):"
    if 'LOAD' in line:
        print "Process LOAD line"
        section = 'LOAD'
    elif 'NAME' in line:
        section=line.strip('\n').split("=")[1] # the section name is the second part of the line
                            # (without the '\n' at the end), splitted with the "="  and
                            # where the first part is NAME
    else:
        if section == 'LIVE':
            print "process %s\n%s" % (section, line)
        elif section == 'SIDL':
            print "process %s\n%s" % (section, line)

This should do part one.

f=open(textFile)
lines=f.readlines()
f.close()
a=len(lines)
i=0
while i < a:
p = re.compile(searchText, re.MULTILINE)
m = p.search(lines)
if(m):
LineNumber = p.tell()
i=i+1


don't quite understand part 2 of ypur question

f=open(textFile)
lines=f.readlines()
f.close()
a=len(lines)
i=0
while i < a:
p = re.compile(searchText, re.MULTILINE)
m = p.search(lines)
if(m):
LineNumber = p.tell()
i=i+1

Hi, GeyC

I think you way is smart. I wil try it later on and update if I find some problem. Thank you so much.

Ning

This question has already been answered. Start a new discussion instead.