Thursday, 25 February 2021

Bulk autoreplacing string in the KML file

I have a set of placemarks, which include quite a wide description included in its balloon within the property. Next each single description (former column header) is bounded in . Because of the shapefile naming restriction to 10 characters only.

https://gis.stackexchange.com/questions/15784/bypassing-10-character-limit-of-field-name-in-shapefiles

I have to retype most of these names manually.

Obviously, I use Notepad++, where I can swiftly press Ctrl+F and toggle Replace mode, as you can see below.

enter image description here

The green bounded strings were already replaced, the red ones still remain.

Basically, if I press "Replace All" then it works fine and quickly. Unfortunately, I have to go one by one. As you can see I have around 20 separate strings to "Replace all". Is there a possibility to do it quicker? Because all the .kml files are similar to each other, this is going to be the same everywhere. I need some tool, which will be able to do auto-replace for these headers cut by 10 characters limit. I think, that maybe Python tools might be helpful.

https://pythonhosted.org/pykml/

But in the tool above there is no information about bulk KML editing.

How can I set something like the "Replace All" tool for all my strings preferably if possible?

UPDATE:

I tried the code below:

 files = []
 with open("YesNF016.kml") as f:
  for line in f.readlines():
    if line[-1] == '\n':
        files.append(line[:-1])
    else:
        files.append(line)

  old_expression = 'ab'
  new_expression = 'it worked'
  for file in files:
  new_file = ""
  with open(file) as f:
    for line in f.readlines():
        new_file += line.replace(old_expression, new_expression)
 with open(file, 'w') as f:
    f.write(new_file)

The debugger shows:

[Errno 22] Invalid argument: '' File "\test.py", line 13, in with open(file) as f:

whereas line 13 is:

  with open(file) as f:

The solutions here:

https://www.reddit.com/r/learnpython/comments/b9cljd/oserror_while_using_elementtree_to_parse_simple/

and

OSError: [Errno 22] Invalid argument Getting invalid argument while parsing xml in python

weren't helpful enough for me.



from Bulk autoreplacing string in the KML file

No comments:

Post a Comment