【问题标题】:TypeError: argument should be a bytes-like object or ASCII string, not 'tuple'TypeError: 参数应该是类似字节的对象或 ASCII 字符串,而不是 \'tuple\'
【发布时间】:2022-11-10 22:08:35
【问题描述】:

我想将字典内容写入文件并保存。 我刚收到这条消息: TypeError: 参数应该是字节类对象或 ASCII 字符串,而不是“元组”

#Creation of dictionary
final_dict = {}
final_dict['file_name']=d['filename'] 
final_dict1 = {} 
final_dict1['binary']=temp
final_dict1['type']=temp1

V10=((['file_name']),(['binary']),(['type']))
print(V10)
(['file_name'], ['binary'], ['type'])

outputfile = open('XXXXX.pptx', 'wb')
outputfile.write(base64.b64decode(V10))
outputfile.close()  
TypeError                                 Traceback (most recent call last)
Input In [34], in <cell line: 2>()
      1 outputfile = open('XXXXX.pptx', 'wb')
----> 2 outputfile.write(base64.b64decode(V1))
      3 outputfile.close()

File ~\Anaconda3\lib\base64.py:80, in b64decode(s, altchars, validate)
     65 def b64decode(s, altchars=None, validate=False):
     66     """Decode the Base64 encoded bytes-like object or ASCII string s.
     67 
     68     Optional altchars must be a bytes-like object or ASCII string of length 2
   (...)
     78     in the input result in a binascii.Error.
     79     """
---> 80     s = _bytes_from_decode_data(s)
     81     if altchars is not None:
     82         altchars = _bytes_from_decode_data(altchars)

File ~\Anaconda3\lib\base64.py:45, in _bytes_from_decode_data(s)
     43     return memoryview(s).tobytes()
     44 except TypeError:
---> 45     raise TypeError("argument should be a bytes-like object or ASCII "
     46                     "string, not %r" % s.__class__.__name__) from None

TypeError: argument should be a bytes-like object or ASCII string, not 'tuple'

注意:我希望写入变量的内容:

{'file_name': 'ABCDERFROOEKWWKE.pptx'} {'binary': 'UEsXAAIXXXXXXXXXXXXXXXAACclAAATAAgCW0NvbnRlbnRfVHlwZXNdLFFFFBAIooAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

【问题讨论】:

  • V10 是一个元组,但 base64 用于字符串。说你想对一个元组进行base64解码是没有意义的
  • 为什么期望它写 UEsXAAIXXXXXXXXXXXXXXXAACclAATAA...?那个字符串是从哪里来的?
  • @user253751, when i print the dictionary (print(final_dict, final_dict1)) i get this output:{'file_name': 'ABCDERFROOEKWWKE.pptx'} {'binary': 'UEsXAAIXXXXXXXXXXXXXXXAACclAAATAAgCW0NvbnRlbnRfVHlwZXNdLFFFFBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA That's what I want you to write to the file.
  • 那你为什么写base64.b64decode(V10)
  • 我错过了在这里复制“0”。我再次运行代码,我得到相同的消息:(

标签: python json dictionary


【解决方案1】:

要写入文件,首先需要将对象转换为strbytes 对象,具体取决于写入模式 (见Methods of File Objects):

with open(file_name, "w") as file:
    file.write(str( my_object ))

如果您想在 base64 中编码(而不是解码)它,只需以二进制模式在 str(my_object).encode() 上调用 b64encode

相反,如果您想在编写字典之前对其值进行解码,则只需构造一个新字典 {k: b64decode(v) for k, v in old_dict.items()}

另外,由于您使用 json 作为标签,请查看 json.dump (Saving structured data with json)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2018-03-17
    • 2017-07-18
    • 1970-01-01
    相关资源
    最近更新 更多