![](/img/trans.png)
[英]<?xml version=“1.0” encoding=“UTF-8”?> not <?xml version='1.0' encoding='UTF-8'?>
[英]Removing BOM characters after adding <?xml version="1.0" encoding="UTF-8"?> to SQL-generated XML file with Python
def append_prologue(file, orgID, schema):
timestamp = datetime.today().strftime('%Y%m%d')
new_name = f'{orgID}_000_2022TSDS_{timestamp}1500_' + schema
new_file = file.parent.parent / 'results/with_prologue' / new_name
if new_file.exists():
print(f'{new_file.name} already exists')
with open(file, 'r') as original:
data = original.read()
data = data[3:] #how the original writer dealt with the issue
with open(new_file, 'w+') as modified:
modified.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + data)
return
编解码器包应该可以解决问题。
StreamReader = codecs.getreader('utf-8-sig')
with StreamReader(open(file, 'rb')) as original:
...
或更短的版本:
with codecs.open(file, 'r', 'utf-8-sig') as original:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.