954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

writing to file using "index"

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 = " -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):

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

232.661082515329 226.070442386027233.294671906327 256.064262174272369.65460238028 224.374211320059329.0034454898 3.55174522463776369.734427409734 235.727500401241
*******************************************************************
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

mbehnaam
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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 = ""
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?

mbehnaam
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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()
-ordi-
Junior Poster in Training
92 posts since Dec 2009
Reputation Points: 18
Solved Threads: 11
 

or

print re.search(r'name="(.*)"', text)
-ordi-
Junior Poster in Training
92 posts since Dec 2009
Reputation Points: 18
Solved Threads: 11
 

You should use proper xml module to read xml data http://wiki.python.org/moin/PythonXml

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Solved, thank you very much!!!

mbehnaam
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Mark it solved then as the original poster.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: