【python – 实现保留docstring的类属性】教程文章相关的互联网学习教程文章

python – 将String转换为datetime时的ValueError【代码】

我有一个数据帧如下,我试图将数据帧减少到只包含Date大于变量curve_enddate的行. df [‘Date’]是在datetime中,因此我正在尝试转换curve_enddate [i] [0],它给出了2015-06-24形式的字符串到datetime但是得到了错误ValueError:time data’2015 -06-24’与格式’%Y-%b-%d’不匹配.Date Maturity Yield_pct Currency 0 2015-06-24 0.25 na CAD 1 2015-06-25 0.25 0.0...

python – StringIO和ByteIO有什么区别?

StringIO和ByteIO有什么区别?您会使用哪种用例?解决方法:顾名思义,StringIO使用str数据,而BytesIO使用字节数据.字节是原始数据,例如65,而str解释这些数据,例如使用ASCII编码65是字母’A’. 当您想要非法地处理数据时,最好使用字节数据 – 即您不关心其中包含的内容.例如,套接字仅传输原始字节数据. 当您想要向用户呈现数据或在更高级别解释时,使用str.例如,如果您知道文件包含文本,则可以直接将原始字节解释为文本.

python – base64 miss encodestring【代码】

import base64 if __name__ == '__main__':res = base64.encodestring('srcdasd') print(res)我在控制台python base64.py中运行,得到以下错误消息. 但我可以正确地在textMate中运行它. AttributeError:’module’对象没有属性’encodestring’解决方法:您的文件名不应该是base64.py,它会与您正在使用的模块发生冲突.重命名您应该解决的文件.

Python time & datetime & string 相互转换

详见代码:#!/usr/bin/python # -*- coding:utf-8 -*-import datetime import time# 日期时间字符串 st = "2017-11-23 16:10:10" # 当前日期时间 dt = datetime.datetime.now() # 当前时间戳 sp = time.time()# 1.把datetime转成字符串 def datetime2String(dt):print("1.把datetime转成字符串: ", dt.strftime("%Y-%m-%d %H:%M:%S"))# 2.把字符串转成datetime def string2Datetime(st):print("2.把字符串转成datetime: ", datetim...

python写算法题:leetcode: 97. Interleaving String

class Solution(object):def isInterleave(self, s1, s2, s3):""":type s1: str:type s2: str:type s3: str:rtype: bool"""if len(s1)+len(s2)!=len(s3):return Falsepath=[]p0=-1p1=-1i=0checked=set()while i<len(s3):matched=0if (p0,p1,i) not in checked:if p0+1<len(s1) and s1[p0+1]==s3[i]:checked.add((p0,p1,i))p0+=1matched=1if p1+1<len(s2) and s2[p1+1]==s3[i]:if matched==1:path.append((i+1,p0,p1+1))else:matche...

与Python中的string.Template相反【代码】

我知道模板可以像下面这样工作:x = Template(" Coordinates; $o1;$o2;$o3;\n") y = x.substitute(o1 = 23, o2 = 108, o3 = 655)你会给我:" Coordinates; 23;108;655;\n"我想知道是否有办法逆转这个?像我打包的东西解压缩:x = Template(" Coordinates; $o1;$o2;$o3;\n") y = " Coordinates; 23;108;655;\n" z = x.unpack(y)并让z返回类似的东西:["23","108","655"]有任何想法吗?我应该使用正则表达式吗?...

python – 有没有办法让StringIO读取阻塞【代码】

我搜索了文档并搜索过,但没有任何关于阻止StringIO对象的说法. 我可以创建自己的文件类对象,只是简单地包装StringIO但是如何阻止它?我知道的唯一方法是使用while循环和time.sleep(0.1)直到有可用的数据.解决方法: import osr, w = os.pipe() r, w = os.fdopen(r, 'rb'), os.fdopen(w, 'wb')完全按照我的需要工作,这个管道功能遗憾地在文档中不是很明显,所以我后来才发现它.

c++ 调用 python函数,不能直接传入string类型,要变成char *类型的参数

Py_Initialize(); //初始化//必须写 PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')");//这一步很重要,修改Python路径//加载函数所i在文件名 PyObject * pModule = PyImport_ImportModule("pythonmain"); //test:Python文件名,若脚本有错则返回空//加载 名为m的函数 PyObject *pfun = PyObject_GetAttrString(pModule, "m");//传入string类型参数,这里注意一定要声明称 char *类型,不能直接传入...

python – Flask WTF’StringField’对象没有属性’translate’【代码】

我是Python的新手,我一直关注Miguel Grinberg Flask Mega-Tutorial. 我有一个非常简单的表单,当我尝试提交时,我收到以下错误:AttributeError: ‘StringField’ object has no attribute ‘translate’这是表格:from flask.ext.wtf import Form from wtforms import StringField from wtforms.validators import DataRequiredclass CreateSubjectForm(Form):name = StringField('name', validators=[DataRequired()])和views.py:...

在Python中一次迭代String字【代码】

我有一个巨大的文本文件的字符串缓冲区.我必须在字符串缓冲区中搜索给定的单词/短语.什么是有效的方法呢? 我尝试使用re模块匹配.但由于我有一个巨大的文本语料库,我必须搜索.这需要花费大量时间. 给出单词和短语词典. 我遍历每个文件,将其读入字符串,搜索字典中的所有单词和短语,并在找到键时增加字典中的计数. 我们认为的一个小优化是将短语/单词的字典排序为最大单词数.然后比较字符串缓冲区中的每个单词起始位置并比较单词列表...

Python cStringIO是线程安全的吗?

正如标题所说,Python cStringIO是否保护其内部结构以供多线程使用? 谢谢.解决方法:看一下优秀的work on explaining GIL,然后注意cStringIO纯粹是用C编写的,它的调用不会释放GIL. 这意味着正在运行的线程在read()/ write()期间不会自动切换(使用当前的虚拟机实现). (操作系统将抢占该线程,但其他Python线程将无法获取GIL.) 看一下源代码:Python-2.7.1 / Modules / cStringIO.c没有提到内部保护.如有疑问,请查看来源:)

python – 使string.replace语句的序列更具可读性【代码】

当我在Python中处理HTML代码时,由于特殊字符,我必须使用以下代码.line = string.replace(line, "&quot;", "\"") line = string.replace(line, "&apos;", "'") line = string.replace(line, "&amp;", "&") line = string.replace(line, "<", "<") line = string.replace(line, ">", ">") line = string.replace(line, "&laquo;", "<<") line = string.replace(line, "&raquo;", ">>") line = string.replace(line, "", "'") line = ...

c – Boost.Python.ArgumentError:python str未转换为std :: string【代码】

我得到一个错误我无法通过Boost.Python包装一个相当简单的c类.一,班级:#include <boost/python.hpp> #include <boost/shared_ptr.hpp> #include <vector> #include <string>class token {public:typedef boost::shared_ptr<token> ptr_type;static std::vector<std::string> empty_context;static std::string empty_center;token(std::string& t = empty_center,std::vector<std::string>& left = empty_context,std::vector<std...

python – 数学表达式中String类型的字符串操作【代码】

想象一下exp(49/200)+(x-49/200)我想传递函数“roundn”的参数,无论是不是加法或减法的操作所以我的表情变成了roundn(exp(roundn(49/200, n)), n) + (x - roundn(49/200, n)我想要操纵的表达是这样的:exp(49/200)+exp(49/200)*(x-49/200)+1/2*exp(49/200)*(x-49/200)^2+1/6*exp(49/200)*(x-49/200)^3+1/24*exp(49/200)*(x-49/200)^4+1/120*exp(49/200)*(x-49/200)^5+1/720*exp(49/200)*(x-49/200)^6+1/5040*exp(49/200)*(x-49/200...

python – 为什么string> int求值为True?【代码】

参见英文答案 > How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists? 2个怎么检查字符串> int评估为True?>>> strver = "1" >>> ver = 1 >>> strver > ver True >>> strVer2 = "whaat" >>> strVer2 > ver True做了一些实验:>>> ver3 = 0 >>> strVer2 > ver3 True我认为在尝试比较时应该有一个错误但似乎没有构建来处理...