应该何时__dunder__
直接调用方法?例如,而不是
a + b
Run Code Online (Sandbox Code Playgroud)
一个人可以写
a.__add__(b)
Run Code Online (Sandbox Code Playgroud)
但这是个好主意吗?
我希望能够遍历其中包含两个子文件夹的根文件夹,转到其中一个子文件夹,对文件进行排序,然后仅对第一个排序的文件运行单独的函数,然后在剩余的文件夹中执行相同的操作。到目前为止,这是我的代码,它不起作用。该AppendFiles
函数永远不会运行,当我在本节末尾放置回溯时,它不会给我任何错误。
x=0
y=0
errorFiles={}
datadir = 'C:/root/path'
for root, dirs, files in os.walk(datadir):
for dirname in dirs:
for filename in sorted(files, key=int):
name = filename[:-4]
file = datetime.strptime(name, "%y%m%d%H")
(x,errorFiles) = AppendFiles(file,datadir,dirname,x,y,errorFiles)
if x==0:
pass
else:
print("The following" + dirname + " files experienced a Decoding Error:\n")
for i in range(x):
print(errorFiles[i] + "\n")
break
Run Code Online (Sandbox Code Playgroud)
这name = filename[:-4]
部分只是去掉.csv
最后的文件名,这样它就能很好地配合我的功能。我不知道从这里去哪里,非常感谢任何和所有的帮助!
附带问题,我的语法正确吗(x,errorFiles)
? AppendFiles()
返回(a,b)
其中b
是一个数组。
在 Python 3 中,我想限制传递给此方法的允许值:
my_request(protocol_type, url)
Run Code Online (Sandbox Code Playgroud)
使用类型提示我可以写:
my_request(protocol_type: str, url: str)
Run Code Online (Sandbox Code Playgroud)
所以协议和 url 仅限于字符串,但我如何验证protocol_type
只接受有限的一组值,例如'http'
和'https'
?
我在 python 中有一个使用字符串列表的枚举类:
from enum import Enum
myEnum = Enum('myEnum',['typeA',typeB','typeC'])
Run Code Online (Sandbox Code Playgroud)
我想将方法“ str ”添加到这个枚举类中:
def __str__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
这样当我使用 str() 函数时,我只得到“typeX”部分,即没有类名的枚举部分。例如
print(str(myEnum.typeA)) # I want this to print "typeA" instead of "myEnum.typeA"
Run Code Online (Sandbox Code Playgroud)
我不知道如何将“ str ”方法添加到此类,因为它不是类定义?感谢您的帮助。
我有以下代码.我正在从csv读取并将其内容解析为列表:
import csv
finput = input.reader(open(path,'rb'),delimiter=',',quotechar='|')
print finput[0]
Run Code Online (Sandbox Code Playgroud)
输出是:
['"1239"', '"2249.00"', '"1"', '"3"', '"2011-02-20"']
Run Code Online (Sandbox Code Playgroud)
现在我想提取1239数...第一个元素,所以print finput [0] [0]给出"1239"
.现在,如果我将其转换为整数:
el = int(finput[0][0])
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
ValueError: invalid literal for int() with base 10: '"1239"'
Run Code Online (Sandbox Code Playgroud)
为什么它有那些双引号?
什么是提取整数的简洁方法1239
?
在尝试从列表中删除不以特定子字符串开头的所有文件名时,我遇到了以下意外行为:
>>> allfiles = os.listdir(mydir)
>>> allfiles
['dwcpybyext.sh', 'dwlaunch', 'libupdate.sh', 'ntpsync.sh']
>>> for f in allfiles:
... if f.startswith('n') == False:
... allfiles.remove(f)
...
>>> allfiles
['dwlaunch', 'ntpsync.sh']
Run Code Online (Sandbox Code Playgroud)
从理论上讲,这应该已经删除了不从列表开始的每个文件名'n'
.相反,它留'd'
在列表中的一个开头.如果我改变循环使用if f.startswith('d') == False:
我得到['dwcpybyext.sh', 'dwlaunch', 'ntpsync.sh']
- 最后一项甚至不包含一个'd'
字符.
为什么我看到这种行为?它似乎不太可能是Python list.remove()
方法中的错误- 如果我替换del allfiles[allfiles.index(f)]
,我会得到相同的行为,并且.remove()
基本上只是一个别名.
我需要为我的模块的树视图添加一个默认过滤器.
我在openerp中看到了一些代码示例,如下所示:
<filter
string="Partner"
icon="terp-partner"
domain="[]"
context="{'group_by':'partner_id'}"
/>
Run Code Online (Sandbox Code Playgroud)
这个purchase
模块partner_id
中的一个,作为一个例子分组.
现在,我有一个自定义模块,当你点击它的菜单时,我需要一个'默认'过滤器.
此过滤器必须显示所有未"过期"的记录,或者在浏览模块中的记录时未通过实际日期的记录.
我在自定义模块中有这个字段:
'Fecha_de_Vence': fields.date(
'Fecha de Vencimiento',
required=True,
select=True,
),
Run Code Online (Sandbox Code Playgroud)
这是我需要作为此模块中所有记录的过滤器的字段.
现在,在"高级搜索"中,我可以举例Fecha de Vencimiento
说actual date
,不过,我需要将其作为"默认"过滤器.
任何人都可以在openerp的xml视图中默认如何获得这个?
我有一个Python 3类方法来重新调整值,如下所示:
class A(object):
"""docstring for A"""
def __init__(self):
super(A, self).__init__()
def rescale(self, old_min, old_max, new_min, new_max, value):
"""rescales a value given a current old_min and
old_max to the desired new_min and new_max"""
scale = (old_max - old_min) / (new_max - new_min)
rescaled_value = new_min + ((value - old_min) / (scale))
return rescaled_value
Run Code Online (Sandbox Code Playgroud)
使用Python 3,此方法的工作方式如下:
>>> import A
>>> x = A()
>>> x.rescale(1,10,1,100,5)
45.0
Run Code Online (Sandbox Code Playgroud)
在Python 2.7中,此代码不起作用:
>>> from __future__ import division
>>> x = A()
>>> x.rescale(1,10,1,100,5)
Traceback (most …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法,在python 3.2中,创建大量变量并为它们赋值.就像是
X = 10
Y = 10
A = 0
B = 0
while X >= 0:
while Y >= 0:
cell[C]X[A] = A
cell[C]Y[B] = B
B = B + 1
Y = Y - 1
C = C + 1
A = A + 1
X = X - 1
Run Code Online (Sandbox Code Playgroud)
哪个最佳地创建200个变量的cell1X1,cell1Y1,cell2X1,cell2Y2等等.
这可能吗?怎么做?
请记住,我仍然是python的新手,所以请尽可能简单.
此外,虽然我知道还有其他方法可以做到这一点,而且他们可能更好,我仍然想知道如何以这种方式做事.
我理解字典可能会以各种可能的方式更好,但这不是我要求的.
谢谢您的帮助.
编辑:当我说新的python时,我的意思是说一般的编程新手.喜欢很新.就像,刚刚学会了如何编写函数.
注意:没有一个建议的答案对我有用,因为它们是 for 循环驱动的(我已经在工作了)并且没有解释列表理解版本有什么问题。
我正在努力转型
('name:80', 'desc:100')
Run Code Online (Sandbox Code Playgroud)
进入
{'name': 80, 'desc': 100}
Run Code Online (Sandbox Code Playgroud)
一个工作for
循环:
new_wrap = {}
for item in wrap:
k, v = item.split(':')
new_wrap[k] = int(v)
wrap = new_wrap
Run Code Online (Sandbox Code Playgroud)
非工作列表理解:
wrap = dict([
(k, int(v))
for item in wrap
for (k, v) in item.split(':')
])
Run Code Online (Sandbox Code Playgroud)
这使
Traceback (most recent call last):
File ..., line ..., in ...
for (k, v) in item.split(':')
ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
python ×10
python-3.x ×5
enums ×2
openerp ×2
openerp-7 ×2
enumeration ×1
file-io ×1
filter ×1
list ×1
loops ×1
methods ×1
os.walk ×1
python-2.7 ×1
type-hinting ×1
variables ×1
xml ×1