我曾尝试
voluptuous和schema,这两者都是在验证简单和伟大的,但他们都做基于异常的错误报告,即它们无法在第一个错误.有没有办法可以在Voluptuous或Schema中获得数据的所有验证错误?
我发现jsonschema似乎匹配了一些要求,但没有验证对象键和基于自定义函数的验证(例如lambdas).
需求:
def myMethod(input_dict):
#input_dict should abide to this schema ->
# { 'id' : INT , 'name':'string 5-10 chars','hobbies': LIST OF STRINGS }
# for incorrect input like
# {'id': 'hello','name':'Dhruv','hobbies':[1,2,3] }
# I should be able to return all errors like
# ['id' is not integer,'hobbies' is not list of strings ]
Run Code Online (Sandbox Code Playgroud) 我有一个包含已知列的输入文件,让我们说两列Name和Sex.有时它有标题行Name,Sex,有时它不会:
1.csv:
Name,Sex
John,M
Leslie,F
Run Code Online (Sandbox Code Playgroud)
2.csv:
John,M
Leslie,F
Run Code Online (Sandbox Code Playgroud)
事先了解了列的身份,是否有一种很好的方法可以使用相同的read_csv命令处理这两种情况?基本上,我想指定names=['Name', 'Sex'],然后header=0只在标题存在时推断它.我能想出的最好的是:
1)在执行之前读取文件的第一行read_csv,并适当地设置参数.
2)只需这样做df = pd.read_csv(input_file, names=['Name', 'Sex']),然后检查零行是否与标题相同,如果是,则删除它(然后可能必须重新编号行).
但这对我来说似乎并不像用例那么不寻常.是否有一种内置的方式来做这个read_csv我没有想到的?
我有一个元组列表,我在一个简单的for循环中循环,以识别包含一些条件的元组.
mytuplist =
[(1, 'ABC', 'Today is a great day'), (2, 'ABC', 'The sky is blue'),
(3, 'DEF', 'The sea is green'), (4, 'ABC', 'There are clouds in the sky')]
Run Code Online (Sandbox Code Playgroud)
我希望它像这样高效可读:
for tup in mytuplist:
if tup[1] =='ABC' and tup[2] in ('Today is','The sky'):
print tup
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,没有打印任何内容.
下面的代码有效,但非常罗嗦.我怎么做它像上面那样?
for tup in mytuplist:
if tup[1] =='ABC' and 'Today is' in tup[2] or 'The sky' in tup[2]:
print tup
Run Code Online (Sandbox Code Playgroud)