Hi,

I am beginner in Python and I'm looking for a method to write to a file by using Index.

This is my program:
**************************************************************
infile = open("land1.txt","r")
outfile = open('out.txt', 'w')
text = infile.read()
infile.close()

search = "<CgPoint"
found=text.find(search)
while found > -1:
print search, "found at location", found
found=text.find(search, found+1)
****************************************************************
This program can give me the index of each line. Then I have extract the coordinates from the line and write it to another file.


and land1.txt is something like this (an XML file):
*************************************************************************************

<CgPoint oID="7824186" state="existing" pntSurv="boundary" name="AX">232.661082515329 226.070442386027</CgPoint>
<CgPoint oID="7825374" state="existing" pntSurv="boundary" name="AN">233.294671906327 256.064262174272</CgPoint>
<CgPoint oID="7825082" state="existing" pntSurv="boundary" name="BE">369.65460238028 224.374211320059</CgPoint>
<CgPoint oID="7816689" state="existing" pntSurv="boundary" name="BV">329.0034454898 3.55174522463776</CgPoint>
<CgPoint oID="7825549" state="existing" pntSurv="boundary" name="BQ">369.734427409734 235.727500401241</CgPoint>
*******************************************************************
Attention: Sometime the above mentioned lines are in one line and whole the file is a very big line.
So I want to extract the red characters and produce something like this:


AX 232.661082515329 226.070442386027
AN 233.294671906327 256.064262174272
BE 369.65460238028 224.374211320059
BV 329.0034454898 3.55174522463776
BQ 369.734427409734 235.727500401241

******************************************************************************

Thanks for your help and advice.
Regards

Recommended Answers

All 6 Replies

I have solved some parts.
But I need more time to solve it completely.

infile = open("land1.txt","r")
outfile = open('out.txt', 'w')
text = infile.read()
infile.close()

search1 = " name="
search2 = "</CgPoint>"
found1=text.find(search1)
found2=text.find(search2)
while found1 > -1:
print search, "found at location", found1
found1=text.find(search1, found1+1)
found2=text.find(search2, found2+1)
s = str(text[found1+6:found2])
outfile.write(s)
outfile.write('\n')
outfile.close()


any advice?

Try regular expression:
http://diveintopython.org/regular_expressions/index.html

Bad example:

import re

infile = open("test.txt","r")
outfile = open('out.txt', 'w')

while True:
  text = infile.readline().strip()
  if text:
    nr = re.findall('[0-9.]+', text)
    print nr[1:]
    continue
  else:
    break

infile.close()

or

print re.search(r'name="(.*)"', text)

Solved, thank you very much!!!

Mark it solved then as the original poster.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.