我正在尝试读取一个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时存在空值� 除和之外的每个字符之间的字符
输出文件:
<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)
目前没有回答
相关问题 更多 >
编程相关推荐