繁体   English   中英

添加后删除 BOM 字符<?xml version="1.0" encoding="UTF-8"?>使用 Python 到 SQL 生成的 XML 文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM