没有编码的Python String参数 [英] Python String argument without an encoding

查看:272
本文介绍了没有编码的Python String参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行这段代码,它不断给出一个错误,说没有编码的字符串参数

Am trying to a run this piece of code, and it keeps giving an error saying "String argument without an encoding"

ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '\0'.encode('utf-8')

任何帮助?

推荐答案

在一个字符串对象到一个 bytearray()

You are passing in a string object to a bytearray():

bytearray(content[current_pos:(final_pos)])

您需要提供一个编码参数(第二个参数)因此可以将其编码为字节。

You'll need to supply an encoding argument (second argument) so that it can be encoded to bytes.

例如,您可以将其编码为UTF-8:

For example, you could encode it to UTF-8:

bytearray(content[current_pos:(final_pos)], 'utf8')

bytearray()文档


可选的参数可以用几种不同的方式初始化数组:

The optional source parameter can be used to initialize the array in a few different ways:


  • 如果是一个字符串,你还必须给出>编码(和可选地,错误)参数; bytearray()然后使用 str.encode()将字符串转换为字节。

  • If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

这篇关于没有编码的Python String参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆