Python 2.7用に作成されたので、Python 3.2用のlibtaxii用のスクリプトを編集しています。私はコンテンツブロックをファイルに書き込む次の関数を使って作業しています。ここでは機能があります:Python 3.2のTypeErrorに関するAttributeError問題
def write_cbs_from_poll_response_11(self, poll_response, dest_dir, write_type_=W_CLOBBER):
for cb in poll_response.content_blocks:
if cb.content_binding.binding_id == CB_STIX_XML_10:
format_ = '_STIX10_'
ext = '.xml'
elif cb.content_binding.binding_id == CB_STIX_XML_101:
format_ = '_STIX101_'
ext = '.xml'
elif cb.content_binding.binding_id == CB_STIX_XML_11:
format_ = '_STIX11_'
ext = '.xml'
elif cb.content_binding.binding_id == CB_STIX_XML_111:
format_ = '_STIX111_'
ext = '.xml'
elif cb.content_binding.binding_id == CB_STIX_XML_12:
format_ = '_STIX12_'
ext = '.xml'
else: # Format and extension are unknown
format_ = ''
ext = ''
if cb.timestamp_label:
date_string = 't' + cb.timestamp_label.isoformat()
else:
date_string = 's' + datetime.datetime.now().isoformat()
filename = gen_filename(poll_response.collection_name,
format_,
date_string,
ext)
filename = os.path.join(dest_dir, filename)
write, message = TaxiiScript.get_write_and_message(filename, write_type_)
if write:
with open(filename, 'w') as f:
f.write(cb.content) # The TypeError is thrown here
print("%s%s" % (message, filename))
私の現在の問題は、変数の一つ、cb.content
は型エラーを投げているということです。
TypeError: must be str, not bytes
は、これは簡単な修正です:私の代わりに、コンバータf.write(cb.content.decode("utf-8"))
を使用しました行し、それははAttributeErrorをスロー:
AttributeError: 'str' object has no attribute 'decode'
ので、インタプリタはしかし、それはそれを認識しない、それは文字列です知っています?私は本当に100%確信していません。
私よりも賢いあなたの皆さんに、ありがとうございました!
'cb.contents'の内容を印刷しましたか? –
おそらく結果は 'for cb in ... 'ループの2回の異なる反復に由来します。 'cb.content'を' str'または 'bytes'オブジェクトとして扱えるようにコードを記述してください。 – glibdud
私はしました。内容は印刷することができますが、私はxmlファイルを扱っているので、その中に奇妙な文字があるかもしれないと仮定しています。 内容は次のとおりです。https://pastebin.com/k2Xed6C9 – Arcsector