简体   繁体   中英

Add line number and count amount with python

I have a file from a geographic information system and I need part of that file to use in arcgis and make a feature there.

This is part of that file:

<PropertyValuePair property="LineControlCategory" value="BDYOR" />
    <PropertyValuePair property="Location" value="|0|LINESTRING (-64.9734423 46.8886667 0, -64.975073 46.9527568 0, -65.015054 46.9900623 0, -65.0746205 47.0712622 0, -65.0990984 47.0740409 0, -65.1456079 47.0551434 0, -65.1505056 47.0451361 0, -65.2100694 47.0445795 0, -65.2010947 47.0712622 0, -65.204359 47.0834863 0, -65.2206776 47.0834863 0, -65.2476043 47.0740409 0, -65.2720821 47.0829311 0, -65.2916649 47.066815 0, -65.3357282 47.0801523 0, -65.3732604 47.0773749 0, -65.4219264 47.0582964 0)" />
    <PropertyValuePair property="ObjectLabelA" value="13" />
    <PropertyValuePair property="LineControlCategory" value="BDYOR" />
    <PropertyValuePair property="Location" value="|0|LINESTRING (-65.4401144 46.9992797 0, -65.4268321 47.0112236 0, -65.4351497 47.030746 0, -65.4126433 47.0448317 0, -65.4213041 47.0585458 0)" />
    <PropertyValuePair property="ObjectLabelA" value="para mil force" />

The python script I have now gives me:

X,Y,Feature,FeatureOrder
-64.9734423,46.8886667,
-64.975073,46.9527568,
-65.015054,46.9900623,
-65.0746205,47.0712622,
-65.0990984,47.0740409,

What I need to make this really work in arcgis I need the line number and order number. So something like this:

X,Y,Feature,FeatureOrder
-64.9734423,46.8886667,1,1
-64.975073,46.9527568,1,2
-65.015054,46.9900623,1,3
Etc
-65.4401144,46.9992797,2,1
-65.4268321,47.0112236,2,2
-65.4351497 47.030746,2,3
-65.4126433,47.0448317,2,4

The first number 1 comes from the first line with “Location” in it. The 2nd number is order of the numbers range in that line. This is the script I have now:

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:
        file.write(line.split("(")[1].split(")")[0].replace(", ", "\n").replace(" 0", ",\n").replace(" ", ","))

f.close()
file.close()

I hope sameone can help me with this greetings Peter

It may be hard to implement given the code. The line

file.write(line.split("(")[1].split(")")[0].replace(", ", "\n").replace(" 0", ",\n").replace(" ", ","))

is unreadable one-liner. It shall be at least be refactored to

content = line.split("(")[1].split(")")[0]
rows = [y.replace(" ", ",") for x in content.split(', ')
                            for y in x.split(' 0')]
file.writelines(rows)

which still is dirty, but clean enough to change the code behaviour:

for i, line in enumerate(f.readlines(), 1):
    if "Location" in line:
        content = line.split("(")[1].split(" 0)")[0]
        rows = [y.replace(" ", ",") for x in content.split(', ')
                                    for y in x.split(' 0')
                                    if  y != '']
        file.writelines(['{},{},{}\n'.format(row,i,j)
                         for j, row in enumerate(rows, 1)])

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