我试图将用户帐户数据从Active Directory推送到我们的MySQL服务器.这完美无缺,但不知何故,字符串最终显示变音符号和其他特殊字符的编码版本.
Active Directory使用以下示例格式返回字符串: M\xc3\xbcller
这实际上是UTF-8编码Müller,但我想写入Müller我的数据库M\xc3\xbcller.
我尝试使用此行转换字符串,但它会在数据库中生成相同的字符串:
tempEntry[1] = tempEntry[1].decode("utf-8")
如果我print "M\xc3\xbcller".decode("utf-8")在python控制台中运行输出是正确的.
有没有办法以正确的方式插入这个字符串?对于想要拥有这种格式的Web开发人员,我需要这种特定的格式,我不知道他为什么不能直接使用PHP转换字符串.
附加信息:我正在使用MySQLdb; 表和列编码是utf8_general_ci
我在 Python 中调用 shell 脚本,它会生成多个子进程。如果该进程在两分钟后没有完成,我想终止该进程及其所有子进程。
有什么办法可以使用 subprocess.run 做到这一点,还是必须重新使用 Popen?由于 run 被阻塞,我无法将 pid 保存在某个地方以在额外的命令中杀死子进程。一个简短的代码示例:
try:
subprocess.run(["my_shell_script"], stderr=subprocess.STDOUT, timeout=120)
except subprocess.TimeoutExpired:
print("Timeout during execution")
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Boost::Spirit 解析 PDDL 文件,但在将先决条件解析为结构时遇到了一些麻烦。我正在努力理解有关如何将条件放入我的结构和递归的 Boost 手册。
我给出了下面的代码片段,应该可以很好地说明问题。一个看起来像这样的字符串必须被解析:
:precondition
(and
(at-pos ?r ?pos)
(not (has-pos ?m ?pos))
)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的代码看起来像这样,但我几乎可以肯定,在没有 Boost::Phoenix 经验的情况下,我不了解 at_c 是如何工作的。
predi_param = '?' >> name_type;
predi = '('
>> name_type
>> +predi_param
>> ')';
literal = (
( '(' >> lit("not") >>
predi [at_c<0>(_val) = false]
>> ')'
)
| predi [at_c<0>(_val) = true]
)
>> ')';
pred_list = ( '(' >> lit("and") >> (*pred_list) >> ')')
| literal;
preconditions = lit(":precondition") >> pred_list;
qi::rule<Iterator, std::string(), ascii::space_type> …Run Code Online (Sandbox Code Playgroud) 我已经想出了这个问题,但经过一些测试后我决定用一些更具体的信息创建一个新问题:
我正从Active Directory中使用python-ldap(和Python 2.7)读取用户帐户.这确实很好用,但我有特殊字符的问题.在控制台上打印时,它们看起来像UTF-8编码的字符串.目标是将它们写入MySQL数据库,但我从一开始就没有将这些字符串写入正确的UTF-8.
示例(fullentries是包含所有AD条目的数组):
fullentries[23][1].decode('utf-8', 'ignore')
print fullentries[23][1].encode('utf-8', 'ignore')
print fullentries[23][1].encode('latin1', 'ignore')
print repr(fullentries[23][1])
Run Code Online (Sandbox Code Playgroud)
用手插入字符串的第二次测试如下:
testentry = "M\xc3\xbcller"
testentry.decode('utf-8', 'ignore')
print testentry.encode('utf-8', 'ignore')
print testentry.encode('latin1', 'ignore')
print repr(testentry)
Run Code Online (Sandbox Code Playgroud)
第一个例子的输出是:
M\xc3\xbcller
M\xc3\xbcller
u'M\\xc3\\xbcller'
Run Code Online (Sandbox Code Playgroud)
编辑:如果我尝试用.replace('\\\\','\\)替换双反斜杠,则输出保持不变.
第二个例子的输出:
Müller
M?ller
'M\xc3\xbcller'
Run Code Online (Sandbox Code Playgroud)
有没有办法让AD输出正确编码?我已经阅读了很多文档,但它们都声明LDAPv3为您提供严格的UTF-8编码字符串.Active Directory使用LDAPv3.
我的老问题这个主题在这里:使用Python将UTF-8字符串写入MySQL
编辑:添加了repr(s)信息