草庐IT

as_bytes

全部标签

python : Get size of string in bytes

我有一个要通过网络发送的字符串。我需要检查它所代表的总字节数。sys.getsizeof(string_name)返回额外的字节。例如sys.getsizeof("a")返回22,而一个字符在python中仅表示为1个字节。有没有其他方法可以找到这个? 最佳答案 如果你想要字符串中的字节数,这个函数应该可以很好地为你做。defutf8len(s):returnlen(s.encode('utf-8'))你得到奇怪数字的原因是因为字符串是python中的实际对象,所以封装在字符串中的是一堆其他信息。这很有趣,因为如果您查看我将字符串编

Python 3 TypeError : must be str, not bytes with sys.stdout.write()

我正在寻找一种从python脚本运行外部进程并在执行期间打印其标准输出消息的方法。下面的代码有效,但在运行时不打印标准输出。当它退出时,我收到以下错误:sys.stdout.write(nextline)TypeError:mustbestr,notbytesp=subprocess.Popen(["demo.exe"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)#PollprocessfornewoutputuntilfinishedwhileTrue:nextline=p.stdout.readline()ifnextline=='

python - 对 JPG 图像进行操作时获取 "cannot write mode P as JPEG"

我正在尝试调整一些图像的大小,其中大部分是JPG。但在一些图像中,我收到了错误:Traceback(mostrecentcalllast):File"image_operation_new.py",line168,inmodifyImagetempImage.save(finalName);File"/Users/kshitiz/.virtualenvs/django_project/lib/python2.7/site-packages/PIL/Image.py",line1465,insavesave_handler(self,fp,filename)File"/Users/ksh

python - PIP 安装 Numpy 抛出错误 "ascii codec can' t decode byte 0xe2"

我在新构建的计算机上安装了新安装的Ubuntu。我刚刚使用apt-get安装了python-pip。现在,当我尝试pipinstallNumpy和Pandas时,它会出现以下错误。我在SO和Google上的很多地方都看到过这个错误,但我一直无法找到解决方案。有人提到这是一个错误,一些线程只是死了......这是怎么回事?Traceback(mostrecentcalllast):File"/usr/bin/pip",line9,inload_entry_point('pip==1.5.4','console_scripts','pip')()File"/usr/lib/python2.

python - 在 virtualenv 中无法 "import matplotlib.pyplot as plt"

我正在虚拟环境中使用flask。我能够使用pip安装matplotlib,并且可以在Pythonsession中importmatplotlib。但是,当我将其导入为matplotlib.pyplotasplt我收到以下错误:>>>importmatplotlib.pyplotaspltTraceback(mostrecentcalllast):File"",line1,inFile"//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/pyplot.py",line109,in_backend_mod,new_fig

python - 类型错误 : can't use a string pattern on a bytes-like object in re. findall()

我正在尝试学习如何从页面中自动获取网址。在以下代码中,我试图获取网页的标题:importurllib.requestimportreurl="http://www.google.com"regex=r'(,+?)'pattern=re.compile(regex)withurllib.request.urlopen(url)asresponse:html=response.read()title=re.findall(pattern,html)print(title)我收到了这个意外错误:Traceback(mostrecentcalllast):File"path\to\file\C

python - 一个类轮 : creating a dictionary from list with indices as keys

我想从给定的列表中创建一个字典,只需一行。字典的键是索引,值是列表的元素。像这样的:a=[51,27,13,56]#givenlistd=one-line-statement#onelinestatementtocreatedictionaryprint(d)输出:{0:51,1:27,2:13,3:56}我对为什么要one行没有任何具体要求。我只是在探索python,想知道这是否可能。 最佳答案 a=[51,27,13,56]b=dict(enumerate(a))print(b)会产生{0:51,1:27,2:13,3:56}e

python - UnicodeDecodeError : 'ascii' codec can't decode byte 0xd1 in position 2: ordinal not in range(128)

我正在尝试处理一个非常大的数据集,其中包含一些非标准字符。根据工作规范,我需要使用unicode,但我很困惑。(而且很可能做错了。)我使用以下方法打开CSV:15ncesReader=csv.reader(open('geocoded_output.csv','rb'),delimiter='\t',quotechar='"')然后,我尝试使用以下代码对其进行编码:name=school_name.encode('utf-8'),street=row[9].encode('utf-8'),city=row[10].encode('utf-8'),state=row[11].encode

python - UnicodeDecodeError : 'ascii' codec can't decode byte 0xef in position 1

我在尝试将字符串编码为UTF-8时遇到了一些问题。我尝试了很多东西,包括使用string.encode('utf-8')和unicode(string),但我得到了错误:UnicodeDecodeError:'ascii'codeccan'tdecodebyte0xefinposition1:ordinalnotinrange(128)这是我的字符串:(。・ω・。)ノ我不知道出了什么问题,知道吗?编辑:问题是按原样打印字符串无法正确显示。另外,当我尝试转换它时出现这个错误:Python2.7.1+(r271:86832,Apr112011,18:13:53)[GCC4.5.2]onli

python - UnicodeDecodeError : 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)

我正在使用NLTK对我的文本文件执行kmeans聚类,其中每一行都被视为一个文档。例如,我的文本文件是这样的:belongfingerdeathpunchhastymikehastywallsjerichojägermeisterrulesrulesbandsfollowperformingjägermeisterstageapproach现在我要运行的演示代码是这样的:importsysimportnumpyfromnltk.clusterimportKMeansClusterer,GAAClusterer,euclidean_distanceimportnltk.corpusfro