![](/img/trans.png)
[英]ValueError: Unhandled object <_io.TextIOWrapper name='world.json' mode='r' encoding='utf-8-sig'>
[英]How to remove <_io.TextIOWrapper name='xyz.txt' mode='w' encoding='UTF-8'> from file?
我为需要处理的输出运行函数时正在创建文件,但是除了所需的输出外,我还得到了<_io.TextIOWrapper name ='xyz.txt'mode ='w'encoding ='UTF-在文本文件的每一行中也将8'>作为输出。
示例代码:
def stimcount():
with open('results.txt', 'w') as f:
for rel_node in root.findall("emospan:CharacterRelation",ns):
if rel_node.attrib['Relation']=="Stimulus":
source = rel_node.attrib['Governor']
target = rel_node.attrib['Dependent']
for span_node in root.findall("emospan:CharacterEmotion",ns):
if span_node.attrib[my_id]==source:
print(span_node.attrib['Emotion'])
if span_node.attrib[my_id]==target:
print(span_node.attrib)
print(span_node.attrib, f, file=f)
我应该添加些什么来防止这种情况发生? 我尝试将(...,encoding ='utf-8')添加到open命令,但是没有效果。
提前致谢!
您出现问题的原因在以下这一行:
print(span_node.attrib, f, file=f)
此调用print
的第二个参数(只是f
)意味着您将在输出中包含要写入的文件对象的字符串表示形式。
只需将此行更改为:
print(span_node.attrib, file=f)
解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.