草庐IT

dds-format

全部标签

Python、.format() 和 UTF-8

我的背景是Perl,但我正在为一个新项目尝试Python和BeautifulSoup。在这个例子中,我试图提取并呈现单个页面中包含的链接目标和链接文本。这是来源:table_row=u'{}{}'.encode('utf-8')link_text=unicode(link.get_text()).encode('utf-8')link_target=link['href'].encode('utf-8')line_out=unicode(table_row.format(link_text,link_target))所有这些对.encode('utf-8')的显式调用都是我为使这项工作

python - 使用 Matplotlib.dates.datestr2num 将 pandas DatetimeIndex 转换为 'float days format'

一些Matplotlib方法需要几天'floatdaysformat'.datestr2num是一个转换器函数,但它与相关的pandas对象有关:In[3]:type(df.index)Out[3]:pandas.tseries.index.DatetimeIndexIn[4]:type(df.index[0])Out[4]:pandas.tslib.TimestampIn[5]:mpl.dates.date2num(df.index)Out[5]:...AttributeError:'numpy.datetime64'objecthasnoattribute'toordinal'这提

python - 在 Python 中打开一个 wave 文件 : unknown format: 49. 出了什么问题?

我尝试使用wave模块打开一个wave文件,但无论我尝试什么,我总是遇到同样的错误。错误所在的行如下:wav=wave.open(f)这是错误信息:Traceback(mostrecentcalllast):File"annotate.py",line47,inplay(file)File"annotate.py",line33,inplaywav=wave.open(f)File"C:\ProgramFiles(x86)\Python\lib\wave.py",line498,inopenreturnWave_read(f)File"C:\ProgramFiles(x86)\Pyth

python - 如何将 string.format 与嵌套字典一起使用

我有一个嵌套的字典:KEYS1=("A","B","C")KEYS2=("X","Y","Z")d=dict.fromkeys(KEYS1,dict.fromkeys(KEYS2,0))我现在想使用格式将它的值嵌入到字符串中,例如print("d['A']['X']={A,X:d}".format(**d))输出:d['A']['X']=0那是行不通的。关于如何正确执行此操作的任何建议? 最佳答案 KEYS1=("A","B","C")KEYS2=("X","Y","Z")d=dict.fromkeys(KEYS1,dict.fro

python - 强制字典键用作 str.format 的参数说明符

我有以下字符串插值:>>>a={'test1.1':5}>>>'test:{test1.1}'.format(**a)KeyError:'test1'它显然失败了,因为format实际上是在尝试访问对象test1及其属性1。有没有办法格式化这个字符串并强制将键值作为字符串?(寻找Python2和3解决方案。) 最佳答案 一个小hack,但它可以解决问题:In[5]:'test:{0[test1.1]}'.format(a)Out[5]:'test:5'在[..]中使用类似字典的索引。0是位置索引,a是第0个参数。如果它是唯一的参数,

python - PostgreSQL 类型错误 : not all arguments converted during string formatting

我在链接到PostgreSQL数据库的psycopg2中执行查询。这是有问题的代码:withopen('dataFile.txt','r')asf:lines=f.readlines()newLines=[line[:-1]forlineinlines]curr=conn.cursor()lineString=','.join(newLines)curr.execute("SELECTfields.fieldkeyFROMfieldsLEFTJOINzoneONzone.fieldkey=fields.fieldkeyWHEREzone.zonekey=%s;",(newLines[0

python - Tensorflow 动态循环神经网络 (LSTM) : how to format input?

我得到了这种格式的一些数据和以下详细信息:person1,day1,feature1,feature2,...,featureN,labelperson1,day2,feature1,feature2,...,featureN,label...person1,dayN,feature1,feature2,...,featureN,labelperson2,day1,feature1,feature2,...,featureN,labelperson2,day2,feature1,feature2,...,featureN,label...person2,dayN,feature1,fe

python - 为什么 Python 的 string.format 不能用 "\x00"填充?

我想用空字符("\x00")填充一个字符串。我知道很多方法可以做到这一点,所以请不要用其他方法回答。我想知道的是:为什么Python的string.format()函数不允许用空值填充?测试用例:>>>"{0:\x01这表明十六进制转义字符通常有效。>>>"{0:\x00但是“\x00”变成了一个空格(“\x20”)。>>>"{0:{1}>>"{0:{1}甚至尝试其他几种方法。>>>"bbb"+"\x00"*7'bbb\x00\x00\x00\x00\x00\x00\x00'这有效,但不使用string.format>>>spaces="{0:>>nulls="{0:\x00>>spa

python - string.format(...,**locals()) 的缩写形式

我通常使用以下模式(如thisquestion中所述):a=1s="{a}".format(**locals())我认为这是编写易于阅读的代码的好方法。有时“链接”字符串格式很有用,以便“模块化”复杂字符串的创建:a="1"b="2"c="{a}+{b}".format(**locals())d="{c}isasum".format(**locals())#d=="1+2isasum"很快,代码就被X.format(**locals())缠住了。为了解决这个问题,我尝试创建一个lambda:f=lambdax:x.format(**locals())a="1"b="2"c=f("{a}

python - 如何在 python str.format 中转义点

我希望能够访问字典中带有str.format()点的键。我该怎么做?例如,不带点的键格式:>>>"{hello}".format(**{'hello':'2'})'2'但是当键中有一个点时它不会:>>>"{hello.world}".format(**{'hello.world':'2'})Traceback(mostrecentcalllast):File"",line1,inKeyError:'hello' 最佳答案 你不能。FormatStringSyntax支持仅整数或有效的Python标识符作为键。来自文档:arg_nam