0

I am trying to take a systematic sample of elements from a large OSM file , but I am getting "a bytes-like object is required, not 'str'" error. Here is the code I am using

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xml.etree.ElementTree as ET  # Use cElementTree or lxml if too slow

OSM_FILE = "chattanooga.osm"  # Replace this with your osm file
SAMPLE_FILE = "sample.osm"

k = 100  # Parameter: take every k-th top level element

def get_element(osm_file, tags=('node', 'way', 'relation')):
    """Yield element if it is the right type of tag
    Reference:
    http://stackoverflow.com/questions/3095434/inserting-newlines-in-xml-file-generated-via-xml-etree-elementtree-in-python
    """
    context = iter(ET.iterparse(osm_file, events=('start', 'end')))
    _, root = next(context)
    for event, elem in context:
        if event == 'end' and elem.tag in tags:
            yield elem
            root.clear()


with open(SAMPLE_FILE, 'wb') as output:
    output.write('<?xml version="1.0" encoding="UTF-8"?>\n')
    output.write('<osm>\n  ')

    # Write every kth top level element
    for i, element in enumerate(get_element(OSM_FILE)):
        if i % k == 0:
            output.write(ET.tostring(element, encoding='utf-8'))

    output.write('</osm>')

And here is the error I am getting

Thank you all


TypeError                                 Traceback (most recent call last)
<ipython-input-26-349a323b1196> in <module>()
     23 
     24 with open(SAMPLE_FILE, 'wb') as output:
---> 25     output.write('<?xml version="1.0" encoding="UTF-8"?>\n')
     26     output.write('<osm>\n  ')
     27 

TypeError: a bytes-like object is required, not 'str'
Ameer B.
  • 115
  • 1
  • 3
  • 12
  • String to file not `open(SAMPLE_FILE, 'wb')`. File output is `StringIO` if used string as `file_source`. `"wb"` (b) meaning binary type accepted/returned. – dsgdfg Sep 01 '16 at 06:41
  • I changed the "wb" to "w", but now I am getting different error --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 29 for i, element in enumerate(get_element(OSM_FILE)): 30 if i % k == 0: ---> 31 output.write(ET.tostring(element, encoding='utf-8')) 32 33 output.write('') TypeError: write() argument must be str, not bytes – Ameer B. Sep 01 '16 at 12:59
  • `ET.tostring(element, encoding='utf-8')` excepted bytearray ! Write file string directly. Don't decode anything, open file with encoding ! – dsgdfg Sep 01 '16 at 14:10

0 Answers0