草庐IT

bytes_write

全部标签

python - 将 .csv 文件从 URL 读取到 Python 3.x - _csv.Error : iterator should return strings, not bytes(您是否以文本模式打开文件?)

我已经为这个简单的问题苦苦挣扎了太久,所以我想我会寻求帮助。我正在尝试将国家医学图书馆ftp站点的期刊文章列表读入Python3.3.2(在Windows7上)。期刊文章位于.csv文件中。我已经尝试了以下代码:importcsvimporturllib.requesturl="ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/file_list.csv"ftpstream=urllib.request.urlopen(url)csvfile=csv.reader(ftpstream)data=[rowforrowincsvfile]这会导致以下错误:Traceba

python - print() 与 sys.stdout.write() : which and why?

我有一个使用sys.stdout.write()和sys.stderr.write()创建输出的脚本。最近我发布了几个关于脚本其他方面的问题,我注意到发布答案的人似乎更喜欢print('sometext',file=sys.stdout)。我对输出的主要关注只是它可以通过shell管道传输到文件中或作为另一个程序的输入。有人可以解释(或指导我引用现有的解释)print()和sys.stdout.write之间的区别,每个应该使用的情况和这些约定合理吗? 最佳答案 "有人可以解释(或引导我到现有的解释)print()和sys.stdo

python - f.write 与打印 >> f

在python中写入文件至少有两种方式:f=open(file,'w')f.write(string)或f=open(file,'w')print>>f,string#inpython2print(string,file=f)#inpython3这两者有区别吗?或者还有其他Pythonic吗?我正在尝试将一堆HTML写入文件,所以我需要通过我的文件编写一堆写/打印语句(但我不需要模板引擎)。 最佳答案 print做了file.write不做的事情,允许您跳过一些基本操作的字符串格式化。它在参数之间插入空格并附加行终止符。print"

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

在我的一台机器上,当我使用google应用程序引擎或django时出现错误。例如:app.yamlapplication:demas1252cversion:1runtime:pythonapi_version:1handlers:-url:/imagesstatic_dir:images-url:/cssstatic_dir:css-url:/jsstatic_dir:js-url:/.*script:demas1252c.pydemas1252c.pyimportcgiimportwsgiref.handlersfromgoogle.appengine.ext.webappimpo

Python3 错误 : TypeError: Can't convert 'bytes' object to str implicitly

我正在learnpythonthehardway中的练习41并不断收到错误:Traceback(mostrecentcalllast):File".\url.py",line72,inquestion,answer=convert(snippet,phrase)File".\url.py",line50,inconvertresult=result.replace("###",word,1)TypeError:Can'tconvert'bytes'objecttostrimplicitly我使用的是python3,而书籍使用的是python2,所以我做了一些更改。这是脚本:#!/usr

python - graph.write_pdf ("iris.pdf") AttributeError : 'list' object has no attribute 'write_pdf'

我的代码是按照google的机器学习类的。两个代码是一样的。我不知道为什么会显示错误。可能是变量的类型是错误的。但是google的代码对我来说是一样的。谁有遇到过这个问题吗?这是错误[012][012]Traceback(mostrecentcalllast):File"/media/joyce/oreo/python/machine_learn/VisualizingADecisionTree.py",line34,ingraph.write_pdf("iris.pdf")AttributeError:'list'objecthasnoattribute'write_pdf'[Fin

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

当我尝试连接它时,当字段包含“ñ”或“´”时,我会收到UnicodeDecodeError。如果包含“ñ”或“´”的字段是最后一个,我不会出错。#...nombre=fabricanombre=nombre.encode("utf-8")+'-'+sector.encode("utf-8")nombre=nombre.encode("utf-8")+'-'+unidad.encode("utf-8")#...returnnombre有什么想法吗?非常感谢! 最佳答案 您正在编码为UTF-8,然后重新-编码为UTF-8。Python只

python - 类型错误 : a bytes-like object is required, 不是 'str'

以下是尝试使用套接字修改用户提供的输入的代码:fromsocketimport*serverName='127.0.0.1'serverPort=12000clientSocket=socket(AF_INET,SOCK_DGRAM)message=input('Inputlowercasesentence:')clientSocket.sendto(message,(serverName,serverPort))modifiedMessage,serverAddress=clientSocket.recvfrom(2048)print(modifiedMessage)clientSo

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中的实际对象,所以封装在字符串中的是一堆其他信息。这很有趣,因为如果您查看我将字符串编

node.js - MongoError : attempt to write outside buffer bounds

我有一个简单的node.js代码,它试图获取对象、填充字段然后更新同一个对象:varMongoClient=require('mongodb').MongoClient,Db=require('mongodb').Db,Server=require('mongodb').Server,ObjectID=require('mongodb').ObjectID;vardb=newDb('testing',newServer('localhost',27017));db.open(function(err,db){varUsers=db.collection('users');Users.f