草庐IT

convert_encoding

全部标签

python - numpy 与 python : convert 3d array to 2d

假设我有一张彩色图像,自然会用Python中的3维数组表示,形状为(nxmx3)并将其称为img。我想要一个新的二维数组,将其称为“narray”,形状为(3,nxm),这样该数组的每一行分别包含R、G和Bchannel的“展平”版本。此外,它应该具有我可以通过类似的方式轻松重建任何原始channel的属性narray[0,].reshape(img.shape[0:2])#sothisshouldreconstructbacktheRchannel.问题是如何从“img”构建“narray”?简单的img.reshape(3,-1)不起作用,因为我不希望元素的顺序。谢谢

python - 统一码编码错误 : 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

这个问题在这里已经有了答案:UnicodeerrorOrdinalnotinrange(1个回答)关闭5年前。我只是想解码类似\uXXXX\uXXXX\uXXXX的字符串。但是我得到一个错误:$pythonPython2.7.6(default,Sep92014,15:04:36)[GCC4.2.1CompatibleAppleLLVM6.0(clang-600.0.39)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>printu'\u041e\u043b\u044c\u0433\u0

python - 'bytes' 对象没有属性 'encode'

在将每个文档插入集合之前,我试图存储盐和散列密码。但是在对盐和密码进行编码时,它显示以下错误:line26,inbefore_insertdocument['salt']=bcrypt.gensalt().encode('utf-8')AttributeError:'bytes'objecthasnoattribute'encode'这是我的代码:defbefore_insert(documents):fordocumentindocuments:document['salt']=bcrypt.gensalt().encode('utf-8')password=document['pa

python - Windows 10 ModuleNotFoundError : No module named 'encodings' 上的致命 Python 错误

我正在我的Windows10笔记本电脑上安装Python,当我尝试运行它时,我得到了这个:FatalPythonerror:Py_Initialize:unabletoloadthefilesystemcodecModuleNotFoundError:Nomodulenamed'encodings'Currentthread0x0000037c(mostrecentcallfirst): 最佳答案 我在Windows10上遇到了同样的问题。这是我修复它的方法:打开“环境变量”(在“系统属性”下)。在打开的窗口中,选择“路径”行,然后

python - 如何在 python 中转义 url_encode 的管道 (|) 符号

我在python中遇到urllib.url_encode问题。用一些代码解释赌注:>>>fromurllibimporturlencode>>>params={'p':'12345&6','l':'ab|cd|ef'}>>>urlencode(params)'p=1+2+3+4+5%266&l=ab%7Ccd%7Cef'我想将管道('|')保留在l参数中。你能告诉我怎么做吗?结果应该是'p=1+2+3+4+5%266&l=ab|cd|ef'PS:我不想手动组合URL,而是使用urlencode。谢谢-帕特 最佳答案 Converta

python : UnicodeEncodeError: 'latin-1' codec can't encode character

我在调用api的情况下,根据api的结果,我为api中的每条记录调用数据库。我的api调用返回字符串,当我对api返回的项目进行数据库调用时,对于某些元素,我收到以下错误。Traceback(mostrecentcalllast):File"TopLevelCategories.py",line267,incursor.execute(categoryQuery,{'title':startCategory});File"/opt/ts/python/2.7/lib/python2.7/site-packages/MySQLdb/cursors.py",line158,inexecut

python - 使用 ndb 的 GAE 错误 - BadQueryError : Cannot convert FalseNode to predicate

我有一个使用python在GoogleAppEngine上运行的应用程序。模型类扩展自ndb(google.appengine.ext.ndb)类。我的一个View对数据库进行异步调用,或多或少类似于:#ExerciseListLogisandbmodelclass#start_current,end_currentaredates#student_idisastring#contentsisalistofkeysexercise_log_query=ExerciseListLog.query(ndb.AND(ExerciseListLog.creation>=start_curren

python / NumPy : Convert list of bools to unsigned int

最快(或最“Pythonic”)的转换方式是什么x=[False,False,True,True]进入12?(如果有这种方法。)如果x是bool值的numpy.array会怎么样?有专门的命令吗?我有一个大型m×nbool数组,其中每个n元素行表示高维特征向量的单个低维哈希。(在上面的示例中,n=4。)我想知道答案以便尽可能地压缩我的数据。谢谢。编辑:感谢您的回复!使用以下测试代码,t=0foriterinrange(500):B=scipy.signbit(scipy.randn(1000,20))forbinB:t0=time.clock()#testcodeheret1=time

python - Perl 或 Python : Convert date from dd/mm/yyyy to yyyy-mm-dd

我需要将CSV文件的列中的许多日期从dd/mm/yyyy格式转换为yyyy-mm-dd格式。例如,17/01/2010应转换为2010-01-17。我如何在Perl或Python中执行此操作? 最佳答案 如果保证您的数据格式正确,只包含DD-MM-YYYY格式的单例日期,那么这行得通:#FIRSTMETHODmy$ndate=join("-"=>reversesplit(m[/],$date));这适用于持有“07/04/1776”的$date,但在“this17/01/2010andthat01/17/2010there”上失败。

python - 在 Windows 中更改 Python 3 中的 “locale preferred encoding”

我正在使用Python3(最近从Python2切换过来)。我的代码通常在Linux上运行,但有时(不经常)在Windows上运行。根据open()的Python3文档,如果未提供encodingarg,则文本文件的默认编码来自locale.getpreferredencoding()。对于我的一个项目,我希望这个默认值是utf-8,不管它运行在什么操作系统上(目前,对于Linux,它始终是UTF-8,但对于Windows则不是)。该项目对open()有很多调用,我不想将encoding='utf-8'添加到所有这些调用中。因此,我想在Windows中更改区域设置的首选编码,如Pytho