{"action":"created","issue":{"url":"https://api.github.com/repos/edsu/pymarc/issues/105","repository_url":"https://api.github.com/repos/edsu/pymarc","labels_url":"https://api.github.com/repos/edsu/pymarc/issues/105/labels{/name}","comments_url":"https://api.github.com/repos/edsu/pymarc/issues/105/comments","events_url":"https://api.github.com/repos/edsu/pymarc/issues/105/events","html_url":"https://github.com/edsu/pymarc/issues/105","id":248707032,"number":105,"title":"Add encoding option to marcxml.record_to_xml","user":{"login":"aaronhelton","id":617997,"avatar_url":"https://avatars1.githubusercontent.com/u/617997?v=4","gravatar_id":"","url":"https://api.github.com/users/aaronhelton","html_url":"https://github.com/aaronhelton","followers_url":"https://api.github.com/users/aaronhelton/followers","following_url":"https://api.github.com/users/aaronhelton/following{/other_user}","gists_url":"https://api.github.com/users/aaronhelton/gists{/gist_id}","starred_url":"https://api.github.com/users/aaronhelton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaronhelton/subscriptions","organizations_url":"https://api.github.com/users/aaronhelton/orgs","repos_url":"https://api.github.com/users/aaronhelton/repos","events_url":"https://api.github.com/users/aaronhelton/events{/privacy}","received_events_url":"https://api.github.com/users/aaronhelton/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-08-08T13:00:44Z","updated_at":"2017-08-08T13:27:05Z","closed_at":null,"body":"Unless I am missing something with respect to the marcxml functionality, the record_to_xml function seems to return text encoded in us-ascii, which causes problems when systems are expecting utf-8 encoding. Tracing this issue to its source revealed that xml.etree.ElementTree.tostring takes an optional encoding parameter, which defaults to us-ascii. I am proposing to be able to pass an optional encoding parameter from marcxml.record_to_xml's invocation of ET.tostring.\r\n\r\nIn my [local fork](https://github.com/dag-hammarskjold-library/pymarc/commit/c3d2d83c694a03e361387db9bbb5bb30ce03c893#diff-99e2290f283e0e153f5411fb9a1a3dbf), I have made the following change:\r\n\r\n    def record_to_xml(record, quiet=False, namespace=False, encoding='us-ascii'):\r\n      node = record_to_xml_node(record, quiet, namespace)\r\n      return ET.tostring(node, encoding=encoding)\r\n\r\nWithout the change, my output for record_to_xml on UTF-8 strings that contain diacritics looks like this:\r\n\r\n```\r\n<record>\r\n    <leader>          22        4500</leader>\r\n    <datafield ind1=\" \" ind2=\" \" tag=\"246\">\r\n      <subfield code=\"a\">Nouvelles-H&#233;brides, communiqu&#233;s par le gouvernement de la France et par le gouvernement du Royaume-Uni de Grande-Bretagne et d'Irlande du Nord :</subfield>\r\n      <subfield code=\"b\">Lois et r&#232;glements promulgu&#233;s pour donner effet aux dispositions de la Convention du 13 juillet 1931 pour limiter la fabrication et r&#233;glementer la distribution des stup&#233;fiants, amend&#233;e par le Protocole du 11 d&#233;cembre 1946</subfield>\r\n    </datafield>\r\n</record>\r\n```\r\nAnd the resulting file ends up with a us-ascii encoding, which causes import of the record to fail on the MARC based system we are using.\r\n\r\nWith the change, I get output that looks like this when I pass the optional encoding:\r\n```\r\n<record>\r\n\t<leader>          22        4500</leader>\r\n\t<datafield ind1=\" \" ind2=\" \" tag=\"246\">\r\n\t\t<subfield code=\"a\">Nouvelles-Hébrides, communiqués par le gouvernement de la France et par le gouvernement du Royaume-Uni de Grande-Bretagne et d'Irlande du Nord :</subfield>\r\n\t\t<subfield code=\"b\">Lois et règlements promulgués pour donner effet aux dispositions de la Convention du 13 juillet 1931 pour limiter la fabrication et réglementer la distribution des stupéfiants, amendée par le Protocole du 11 décembre 1946</subfield>\r\n\t</datafield>\r\n</record>\r\n```\r\nI invoke as follows:\r\n\r\n    out_file.write(marcxml.record_to_xml(record,encoding='utf-8'))\r\n\r\nAnd the resulting file ends up with a utf-8 encoding.\r\n\r\nNote that I tried forcing encoding to utf-8 at each successive level beginning with the open() function and working backward to the record itself. The only thing I found that actually works is to pass an encoding parameter in this particular function. If I am missing something (obvious or not), I'd be interested in correcting my oversight.\r\n\r\nThe change looks trivial to me and preserves the default functionality, but I don't know if there are tests that depend on it. "},"comment":{"url":"https://api.github.com/repos/edsu/pymarc/issues/comments/320955386","html_url":"https://github.com/edsu/pymarc/issues/105#issuecomment-320955386","issue_url":"https://api.github.com/repos/edsu/pymarc/issues/105","id":320955386,"user":{"login":"aaronhelton","id":617997,"avatar_url":"https://avatars1.githubusercontent.com/u/617997?v=4","gravatar_id":"","url":"https://api.github.com/users/aaronhelton","html_url":"https://github.com/aaronhelton","followers_url":"https://api.github.com/users/aaronhelton/followers","following_url":"https://api.github.com/users/aaronhelton/following{/other_user}","gists_url":"https://api.github.com/users/aaronhelton/gists{/gist_id}","starred_url":"https://api.github.com/users/aaronhelton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaronhelton/subscriptions","organizations_url":"https://api.github.com/users/aaronhelton/orgs","repos_url":"https://api.github.com/users/aaronhelton/repos","events_url":"https://api.github.com/users/aaronhelton/events{/privacy}","received_events_url":"https://api.github.com/users/aaronhelton/received_events","type":"User","site_admin":false},"created_at":"2017-08-08T13:27:05Z","updated_at":"2017-08-08T13:27:05Z","body":"We're using Invenio, but I don't really know what the internals are doing, since I don't have access to the source code our vendor is maintaining. \r\n\r\nIt's not that the system rejected the unicode characters. It's that the output file ended up with a MIME encoding of us-ascii (as reported by the file --mime-encoding command), and the Invenio batch uploader module rejected it as not being encoded properly. \r\n\r\nAgreed that utf-8 should be default for XML encoding, which is why I find the [function description](https://docs.python.org/3.4/library/xml.etree.elementtree.html#functions) in ET.tostring so strange:\r\n\r\n\r\n>xml.etree.ElementTree.tostring(element, encoding=\"us-ascii\", method=\"xml\", *, short_empty_elements=True)\r\n\r\n>Generates a string representation of an XML element, including all subelements. element is an Element instance. encoding [1] is the output encoding (default is US-ASCII). Use encoding=\"unicode\" to generate a Unicode string (otherwise, a bytestring is generated). method is either \"xml\", \"html\" or \"text\" (default is \"xml\"). short_empty_elements has the same meaning as in ElementTree.write(). Returns an (optionally) encoded string containing the XML data."}}
{"actor":{"display_login":"aaronhelton"}}