Python 3.x Python 3 StringIO没有克隆

Python 3.x Python 3 StringIO没有克隆,python-3.x,Python 3.x,以下代码在Python2中工作,但在Python3中不工作 import http.client from io import StringIO if __name__ == '__main__': res=http.client.HTTPMessage(StringIO(u"headers")) print(str(res)) []$ python3 test.py Traceback (most recent call last): File

以下代码在Python2中工作,但在Python3中不工作

import http.client
from io import StringIO
    
if __name__ == '__main__':
   res=http.client.HTTPMessage(StringIO(u"headers"))
   print(str(res))

[]$ python3 test.py
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print(str(res))
  File "/usr/lib64/python3.9/email/message.py", line 135, in __str__
    return self.as_string()
  File "/usr/lib64/python3.9/email/message.py", line 158, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib64/python3.9/email/generator.py", line 97, in flatten
    policy = policy.clone(max_line_length=self.maxheaderlen)
AttributeError: '_io.StringIO' object has no attribute 'clone'
导入http.client
从io导入StringIO
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
res=http.client.HTTPMessage(StringIO(u“headers”))
打印(str(res))
[]$python3 test.py
回溯(最近一次呼叫最后一次):
文件“test.py”,第9行,在
打印(str(res))
文件“/usr/lib64/python3.9/email/message.py”,第135行,在__
返回self.as_string()
文件“/usr/lib64/python3.9/email/message.py”,第158行,格式为字符串
g、 展平(自,unixfrom=unixfrom)
文件“/usr/lib64/python3.9/email/generator.py”,第97行,扁平化
policy=policy.clone(最大行长=self.maxheaderlen)
AttributeError:“\u io.StringIO”对象没有属性“clone”
我目前正在将旧的Python2代码移植到Python3。
这是对我遇到的一个问题的虚拟测试。

这是否回答了你的问题?不,这是导入StringIO的问题。在本例中,缺少的“克隆”不在“Python3”中,而是在“Python2”中。