我有一个如下的sql查询字符串:
intro text,
id int,
description varchar(50)
我正在尝试创建一个类型字符串,目的是查找与sql模式中定义的类型不匹配的文本。我从sql文本中提取类型的方式如下:
types = [re.sub('[^a-zA-Z]','',x.split()[1]) for x in schema]
types = [re.sub('varchar',types.StringType,x) for x in types]
types = [re.sub('text',types.StringType,x) for x in types]
types = [re.sub('bigint',types.IntType,x) for x in types]
types = [re.sub('decimal',types.IntType,x) for x in types]
但是口译员抱怨说
types = [re.sub('varchar',types.StringTypes,x) for x in types]
AttributeError: 'list' object has no attribute 'StringTypes'
SSCCE
使用以下架构文件
intro text,
id int,
description varchar(50)
和代码(注意,如下面的奥斯卡建议的那样已修复,但现在有其他错误)
import csv
import sys
import re
import types
sch = open(sys.argv[1], "rb")
#---------------------------------
# read schema
#---------------------------------
with sch as f:
schema = f.read().splitlines()
#---------------------------------
# extract schema types
#---------------------------------
foundtypes = [re.sub('[^a-zA-Z]','',x.split()[1]) for x in schema]
foundtypes = [re.sub('varchar',str,x) for x in foundtypes]
foundtypes = [re.sub('text',str,x) for x in foundtypes]
foundtypes = [re.sub('int',int,x) for x in foundtypes]
foundtypes = [re.sub('bigint',int,x) for x in foundtypes]
foundtypes = [re.sub('decimal',int,x) for x in foundtypes]
print foundtypes
我正在使用Python 2.7.5
谢谢
最佳答案
您在以下行中将绑定(请参见:variable shadowing)改写为types
module:
types = [re.sub('[^a-zA-Z]','',x.split()[1]) for x in schema]
之后,
types
不再指向模块,而是指向列表。只需在所有作业中使用另一个名称即可:my_types = [re.sub('[^a-zA-Z]','',x.split()[1]) for x in schema]
my_types = [re.sub('varchar',types.StringType,x) for x in my_types]
my_types = [re.sub('text',types.StringType,x) for x in my_types]
my_types = [re.sub('bigint',types.IntType,x) for x in my_types]
my_types = [re.sub('decimal',types.IntType,x) for x in my_types]
更新
我认为您对解决方案进行了过度设计,除了第一行以外,这不适合使用正则表达式。一个简单的
if-elif-else
可以正常工作:def transform(typestr):
if typestr in ('varchar', 'text'):
return types.StringType
elif typestr in ('int', 'bigint', 'decimal'):
return types.IntType
else:
return None
my_types = [re.sub(r'[^a-zA-Z]', '', x.split()[1]) for x in schema]
[transform(x) for x in my_types]
=> [<type 'str'>, <type 'int'>, <type 'str'>]
关于python - 理解期间列表中的python类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22952000/