使用Python 2.7和SQLAlchemy 0.7,我使用以下命令连接到MySQL DB:
engine = create_engine('mysql://username:password@host/dbname?charset=utf8',echo=False)
Run Code Online (Sandbox Code Playgroud)
根据SQLAlchemy文档,设置charset = utf8会自动隐含use_unicode = 1,因此所有字符串都应该以unicode形式返回. http://docs.sqlalchemy.org/en/rel_0_7/dialects/mysql.html专门给出了这个例子
#set client encoding to utf8; 所有字符串都以unicode create_engine('mysql + mysqldb:/// mydb?charset = utf8')的形式返回
那么,为什么当我在映射类中查询文本字段时,该字段最终是否为"str"类型?
Base = declarative_base(engine)
class RegionTranslation(Base):
''''''
__tablename__ = 'RegionTranslation'
__table_args__ = {'autoload':True}
def __init__(self, region_id, lang_id, name):
self.region_id = region_id
self.lang_id = lang_id
self.name = name
rtrans = session.query(RegionTranslation).filter_by(region_id = 1, lang_id = 6).one()
print (type(rtrans.name))
Run Code Online (Sandbox Code Playgroud)
输出是
<type 'str'>
Run Code Online (Sandbox Code Playgroud)
如果我只是接受这个并在使用之前对字符串进行解码,那么事情就好了.但我不明白为什么上面的代码没有返回类型'unicode'.有人可以请解释一下吗?
我有一个带有此映射的索引:
curl -XPUT 'http://localhost:9200/origindex/_mapping/page' -d '
{
"page" : {
"properties" : {
"title" : {"type" : "text"},
"body" : {"type" : "text"},
"other": {"type": "text"}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
在新索引中,我想将“title”复制到“title1”和“title2”,将“body”复制到“body1”和“body2”(不考虑“other”),并将类型从“page”更改为“文章_eng”。新索引具有以下映射:
curl -XPUT 'http://localhost:9200/newindex/_mapping/articles_eng' -d '
{
"articles_eng" : {
"properties" : {
"title1" : {
"type" : "text",
"analyzer" : "my_analyzer1"
},
"title2" : {
"type" : "text",
"analyzer": "my_analyzer2"
},
"body1": {
"type" : "text",
"analyzer": "my_analyzer1"
},
"body2" : {
"type" : "text",
"analyzer": "my_analyzer2"
}
}
} …Run Code Online (Sandbox Code Playgroud) 我有这个目录结构:
% ls /tmp/source_dir/
aa-aa/ bb-bb/ cc-cc/ morejunk/ somejunk/
% ls /tmp/dest_dir
aa-aa/ bb-bb/ blah/ blahblah/
Run Code Online (Sandbox Code Playgroud)
对于 dest_dir 中匹配 ??-?? 的每个目录,我想从 source_dir 复制相应的文件“goodfile”。我已尝试以下方法但无济于事:
% cd /tmp/dest_dir
/tmp/dest_dir% \ls -d ??-?? | xargs cp /tmp/source_dir/{}/goodfile {}
cp: cannot stat `/tmp/source_dir/{}/goodfile': No such file or directory
cp: cannot stat `{}': No such file or directory
cp: omitting directory `aa-aa'
/tmp/dest_dir% \ls -d ??-?? | xargs bash -c "cp /tmp/source_dir/{$0}/goodfile {$0}"
cp: cannot stat `/tmp/source_dir/{/bin/bash}/goodfile': No such file or directory
Run Code Online (Sandbox Code Playgroud)
当然有一种方法可以做到这一点而无需编写单独的脚本吗?