Python写入错误字符串的文件

2024-03-28 14:57:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试读取一个xml文件,替换一些文本,然后在该文件上进行写操作

输入文件:

<text>A&Z</text>

资料来源:

with open(file, 'rb') as f:
    newText=f.read().decode('utf-8', 'ignore')
    newText= newText.replace("&","and")
with open(relative_file_path+'/'+fileP, "wb") as f:
    print('nt',newText)
    f.write(newText.encode('utf-8'))

打印nt:

nt <�t�e�x�t�>�A�and�Z�<�/�t�e�x�t�>�

打印nt时存在空值� 除和之外的每个字符之间的字符

enter image description here

输出文件:

<text>A湡dZ</text>

我使用decode('utf-8','ignore'),因为在我的xml中有一个无效的开始字符,需要它来读取文件


已解决

谢谢大家的帮助

def stripped(stripstring):
    mpa = dict.fromkeys(range(32))
    stripstring =  stripstring.translate(mpa)
    return stripstring

with open(relative_file_path+'/'+fileP, mode='rb') as f:
    newText=f.read().decode('utf-8-sig', 'ignore')
    newText = stripped(newText)
    newText= newText.replace("&","and")

with open(relative_file_path+'/'+fileP, "w") as f:
    f.write(newText)

Tags: and文件pathtextaswithopenutf