草庐IT

utf8Bytes

全部标签

python - 如何使用 Apache Kafka 修复 Python2.7 中的 "AssertionError: Value must be bytes"错误

我是ApacheKafka技术的新手。我正在尝试使用python2.7将消息作为JSON对象发送到kafka主题,但出现“AssertionError:Valuemustbebytes”错误。我可以成功地以字符串形式发送消息,我可以使用kafka-console-consumer.sh查看我的消息。我正在使用apachekafka2.10-0.8.2.1版本。我在下面给出我的代码。fromkafkaimportKafkaProducerimportyamlproducer=KafkaProducer(bootstap_servers="localhost:9092")msg=yaml.

python - 呈现形式 ('utf-8' 中的 Django UnicodeEncodeError )

我在使用表单呈现页面时遇到UnicodeEncodeError。UnicodeEncodeErrorat/individual/acc/'ascii'codeccan'tencodecharacteru'\u0142'inposition2:ordinalnotinrange(128)这是HTML的片段(表单的标准用法):{{user_form.as_table}}{{individual_form.as_table}}在我看来,我是这样创建它们的:user_form=UserCreationForm(request.POST)individual_form=IndividualFor

python - 如何在 Python 中编码 (utf8mb4)

如何在Python中对ut8mb4中的内容进行编码?我有两组数据:我正在从Parse迁移到我的新MySQL数据库的数据,以及future的数据(仅与我的新数据库对话)。我的数据库是utf8mb4以存储表情符号和带重音符号的字母。只有在我的python脚本中,第一组数据才能正确显示(当涉及表情符号和口音时):MySQLdb.escape_string(unicode(xstr(data.get('message'))).encode('utf-8'))当用PHP读取MySQL数据库时:$row["message"]=utf8_encode($row["message"]);只有当我不包含

python - TypeError : must be string without null bytes, 不是 str

我正在尝试运行这段代码,对我拥有的每一帧运行相同的命令(几乎没有变化):traj.reset()importos#os.chdir(outname)fori,frameinenumerate(traj):frame.superpose()comando="pythonhollow.py-cconstraint-ohollow_%s.pdburei%s.pdb"%(i,i)os.system(comando)pml_cmd="pymolurei%s.pdbhollow_%s.pdb-c-d'ascartoon,urei%s;colorgray90,urei%s;centerchainA;

python - FTP 库错误 : got more than 8192 bytes

Python在上传大小超过8192字节的文件时失败。而异常(exception)只是“得到超过8192个字节”。是否有上传更大文件的解决方案。try:ftp=ftplib.FTP(str_ftp_server)ftp.login(str_ftp_user,str_ftp_pass)exceptExceptionase:print('Connectingftpserverfailed')returnFalsetry:print('Uploadingfile'+str_param_filename)file_for_ftp_upload=open(str_param_filename,'r

Python str(u'a') 和 u'a'.encode ('utf-8' 之间有什么区别)

如标题,有没有理由不使用str()将unicode字符串转换为str?>>>str(u'a')'a'>>>str(u'a').__class__>>>u'a'.encode('utf-8')'a'>>>u'a'.encode('utf-8').__class__>>>u'a'.encode().__class__更新:感谢您的回答,也不知道我是否使用特殊字符创建字符串它会自动转换为utf-8>>>a='€'>>>a.__class__>>>a'\xe2\x82\xac'也是python3中的Unicode对象 最佳答案 当您编写st

python - "Can' t 初始化字符集utf8mb4"with Windows mysql-python

尝试从Windows7客户端通过python2.7+MySQLdb1.2.5+sqlalchemy1.0.9。这是最近将服务器的默认字符集更改为utf8mb4的结果。服务器正在运行MySQL5.5.50。我这样连接:DB_ENGINE=sqlalchemy.create_engine("mysql+mysqldb://{user}:{pass}@{host}:{port}/{database}?charset=utf8mb4".format(**DB_SETTINGS))Session=sqlalchemy.orm.sessionmaker(bind=DB_ENGINE)错误是:Fil

python - Unicode解码错误: 'utf8' codec can't decode byte "0xc3"

在python2.7中我有这个:#-*-coding:utf-8-*-fromnltk.corpusimportabcwithopen("abc.txt","w")asf:f.write("".join(i.words()))然后我尝试在Python3中阅读此文档:withopen("abc.txt",'r',encoding='utf-8')asf:f.read()只为得到:File"C:\Python32\lib\codecs.py",line300,indecode(result,consumed)=self._buffer_decode(data,self.errors,fin

python 3 : reading bytes from stdin pipe with readahead

我想读取字节。sys.stdin以文本模式打开,但它有一个可用于读取字节的缓冲区:sys.stdin.buffer.我的问题是,当我将数据通过管道传输到Python时,如果我想要预读,我似乎只有2个选项,否则我会得到一个io.UnsupportedOperation:Fileorstreamisnotseekable.从sys.stdin读取缓冲文本,将该文本解码为字节,然后寻找(sys.stdin.read(1).decode();sys.stdin.seek(-1,io.SEEK_CUR).由于输入流中的不可编码字节而无法接受。使用peek从stdin的缓冲区中获取一些字节,将其切

Python 3.6 urllib 类型错误 : can't concat bytes to str

我正在尝试使用Python3.6中的urllib从API中提取一些JSON数据。它需要传递header信息以进行授权。这是我的代码:importurllib.request,jsonheaders={"authorization":"Bearer{authorization_token}"}withurllib.request.urlopen("{api_url}",data=headers)asurl:data=json.loads(url.read().decode())print(data)我得到的错误信息是:Traceback(mostrecentcalllast):File"