简体   繁体   中英

Search a text file and print only a part of line to multiple txt files in Python?

I have a xml file with locations on it.

<OverlayObject>
    <PropertyValuePair property="LineControlCategory" value="BDYOR" />
    <PropertyValuePair property="Location" value="|0|LINESTRING (-65.440101 46.999328 0, -65.4018205 46.9315888 0, -65.2459708 46.866914 0, -65.1127481 46.8361129 0)" />
    <PropertyValuePair property="ObjectLabelA" value="13" />
  <OverlayObject>
    <PropertyValuePair property="LineControlCategory" value="BDYOR" />
    <PropertyValuePair property="Location" value="|0|LINESTRING (-65.440101 46.9992462 0, -65.4759729 47.0003057 0, -65.4817235 47.0098503 0, -65.4943889 47.0195827 0)" />
    <PropertyValuePair property="ObjectLabelA" value="9" />
          </OverlayObject>

I want my script to search for each line were the location are and write that line to different txt files. so for the text it would by

file1.txt

<PropertyValuePair property="Location" value="|0|LINESTRING (-65.440101 46.999328 0, -65.4018205 46.9315888 0, -65.2459708 46.866914 0, -65.1127481 46.8361129 0)" />ode here

file2.txt

<PropertyValuePair property="Location" value="|0|LINESTRING (-65.440101 46.9992462 0, -65.4759729 47.0003057 0, -65.4817235 47.0098503 0, -65.4943889 47.0195827 0)" />

The goal is to have the files like this:

file1.txt

    -65.440101,46.999328
 -65.4018205,46.9315888
 -65.2459708,46.866914
 -65.1127481,46.8361129

file2.txt

    -65.440101,46.9992462
 -65.4759729,47.0003057
 -65.4817235,47.0098503
 -65.4943889,47.0195827

So far I have this:

with open('OVERLAY.ovl', 'r') as searchfile:
for line in searchfile:
    if "Location" in line:
        print line

that gives me only the line with the words "location" in it

and i have this:

with open('OVERLAY.ovl', 'r') as input:
for index, line in enumerate(input):
    with open('filename{}.txt'.format(index), 'w') as output:
        if "location" in output:
            output.write(line)

this is writing all line to a different txt file.

how do I combine the 2 scripts and even better can anyone help me to my end goal.

Use lxml and XPath to get the elements you are interested in.

from lxml import etree

x = open("your-file.xml").read()
tree = etree.fromstring(x)
for location in tree.xpath('//PropertyValuePair[@property="Location"]/@value'):
    print(location.split("(")[1].split(")")[0].replace(", ", "\n"))

Output:

-65.440101 46.999328 0
-65.4018205 46.9315888 0
-65.2459708 46.866914 0
-65.1127481 46.8361129 0
-65.440101 46.9992462 0
-65.4759729 47.0003057 0
-65.4817235 47.0098503 0
-65.4943889 47.0195827 0

Saving this to files is left as an exercise for the reader.

After same study i have the code i want without the use of other modules:

f = open('OVERLAY.ovl','r')
file = open("newfile.txt", "w")

file.write("X,Y,Feature,FeatureOrder\n")

for line in f.readlines():
    if "Location" in line:
        print(line.split("(")[1].split(")")[0].replace(", ", "\n").replace(" 0", "").replace(" ", ","))
        file.write(line.split("(")[1].split(")")[0].replace(", ", "\n").replace(" 0", ",\n").replace(" ", ","))

f.close()
file.close()

I now have this file what i can use in arcgis

X,Y,Feature,FeatureOrder

-64.9734423,46.8886667,

-64.975073,46.9527568,

-65.015054,46.9900623,

-65.0746205,47.0712622,

-65.0990984,47.0740409,

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