简体   繁体   English

从python中的.ini文件读取特殊字符文本

[英]reading special characters text from .ini file in python

I am running a script which takes a text "rAh%19u^l\\&G" ie which contains special characters as seen.我正在运行一个脚本,该脚本采用文本“rAh%19u^l\\&G”,即包含所见的特殊字符。

When i pass this text in my script as a argument it runs fine without any error.当我在脚本中将此文本作为参数传递时,它运行良好,没有任何错误。

example - : ./abc.py <username><pwd>示例 - : ./abc.py <username><pwd>

The above text is basically a password.上面的文字基本上就是一个密码。

Now, when i place my values in a config file and read the above text, the script fails .现在,当我将我的值放入配置文件并阅读上述文本时,脚本失败了

*******abc.ini ******* *******abc.ini *******

[DEFAULT]
username = rahul
pwd =  rAh%19u^l\&G

it says它说

/bin/sh:M command not found.

Reading the above values with help of config parser在配置解析器的帮助下读取上述值

******Below is the program abc.py ****** ******下面是程序abc.py ******

#! /usr/bin/python

parser = configparser.ConfigParser()
parser.read('abc.ini')
username = parser.get('DEFAULT','username')
pwd = parser.get('DEFAULT','pwd')


p = subprocess.Popen(
    "abc.py {0} {1}" .format(username, pwd), 
    shell=True, 
    stdout=subprocess.PIPE
)

out, err = p.communicate()

print(out)

I tried searching a lot but found nothing concrete.我尝试了很多搜索,但没有找到具体的内容。

So the question is how to read a text that contains special characters in a .ini file.所以问题是如何读取 .ini 文件中包含特殊字符的文本。

Looks like the % character is the problem here.看起来%字符是这里的问题。 It has special meaning if you are using ConfigParser .如果您使用的是ConfigParser它具有特殊意义。 If you are not using interpolation, then use just RawConfigParser instead, otherwise you must escape the % by doubling it.如果您不使用插值, RawConfigParser使用RawConfigParser ,否则您必须通过加倍来转义%

When I try the example file with ConfigParser it will blow with the following exception:当我使用ConfigParser尝试示例文件时,它会出现以下异常:

InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%19u^l\\&G"'

If I replace ConfigParser with RawConfigParser everything is fine.如果我取代ConfigParserRawConfigParser一切都很好。

The error you posted has nothing to do with it.您发布的错误与此无关。 We can't even tell if it is a python exception or a shell error message.我们甚至无法判断它是 python 异常还是 shell 错误消息。 Please update your question with the full error message.请使用完整的错误消息更新您的问题。 You may also want to check the sh module , a higher level wrapper around subprocess .您还可能要检查的sh模块,围绕更高层次的包装subprocess

Adding up on Paulo Scardine's comment.加上 Paulo Scardine 的评论。

if you have special characters that need to be handled, you can set the ConfigParser 's interpolation argument to None and you won't have the error anymore.如果您有需要处理的特殊字符,您可以将ConfigParserinterpolation参数设置为None并且您将不再有错误。 ConfigParser has interpolation set to BasicInterpolation() by default.默认情况下, ConfigParserinterpolation设置为BasicInterpolation()

You can read more about this here: https://docs.python.org/3.6/library/configparser.html#interpolation-of-values您可以在此处阅读更多相关信息: https : //docs.python.org/3.6/library/configparser.html#interpolation-of-values

Further, as per the docs RawConfigParser is a Legacy variant of the ConfigParser with interpolation disabled by default and unsafe add_section and set methods.此外,根据文档RawConfigParserRawConfigParserLegacy variant of the ConfigParser with interpolation disabled by default and unsafe add_section and set methods.

Here's a snippet from there:这是那里的一个片段:

Example:例子:

[Paths]
home_dir: /Users
my_dir: %(home_dir)s/lumberjack
my_pictures: %(my_dir)s/Pictures

In the example above, ConfigParser with interpolation set to BasicInterpolation() would resolve %(home_dir)s to the value of home_dir (/Users in this case).在上面的示例中,插值设置为 BasicInterpolation() 的 ConfigParser 会将%(home_dir)s解析为home_dir的值(在本例中为 /Users)。 %(my_dir)s in effect would resolve to /Users/lumberjack . %(my_dir)s实际上会解析为/Users/lumberjack [....] [....]

With interpolation set to None , the parser would simply return %(my_dir)s/Pictures as the value of my_pictures and %(home_dir)s/lumberjack as the value of my_dir .将插值设置为None ,解析器将简单地返回%(my_dir)s/Pictures作为my_pictures的值和%(home_dir)s/lumberjack作为my_dir的值。

you could escape the special char with same char.您可以使用相同的字符转义特殊字符。 for example, to be able to read % you have to write it %%例如,为了能够读取 % 你必须写它 %%

I had came across this same issue.我遇到过同样的问题。 Understandably the answer has been posted.可以理解,答案已经发布。 Using the comments from everyone on this post I had resolved this issue with the following:使用这篇文章中每个人的评论,我已经通过以下方式解决了这个问题:

My password was Sgf%ts54hhGtrf&yhgf我的密码是Sgf%ts54hhGtrf&yhgf

alembic ini file I was using: sqlalchemy.url = driver://myuser:Sgf%ts54hhGtrf&yhgf@localhost/my_application我使用的sqlalchemy.url = driver://myuser:Sgf%ts54hhGtrf&yhgf@localhost/my_application ini 文件sqlalchemy.url = driver://myuser:Sgf%ts54hhGtrf&yhgf@localhost/my_application

Error received: configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', which informs me the special character is '%'收到错误: configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(',它告诉我特殊字符是 '%'

Resolved the issue by just wrapping the password in a single quote and doubled the % symbol as recommended from comments in this post to end up with:解决了这个问题,只需将密码用单引号括起来,然后按照本文评论中的建议将 % 符号加倍,最终得到:

sqlalchemy.url = driver://myuser:'Sgf%%ts54hhGtrf&yhgf'@localhost/my_application

No need to worry about changing the ConfigParser as long as you stick to the rule of wrapping the password containing special characters with a single quote and any special characters found in the error you receive to double it eg if the error is related to a % , add another and end up with %%无需担心更改ConfigParser ,只要您坚持使用单引号将包含特殊字符的密码和在您收到的错误中发现的任何特殊字符加倍的规则,例如如果错误与%相关,添加另一个并以%%结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:文本文件读取特殊字符问题 - Python: Text File Reading Special Characters Issues 如何在从文本文件中读取正则表达式时阻止Python转义特殊字符? - How to prevent Python from escaping special characters when reading a regex from a text file? 读取包含python中特殊字符的逗号分隔文本文件 - reading a comma separtated text file containing special characters in python Python:从文件中读取和替换字符串(带有特殊字符)时出错 - Python: Error while reading and replacing String(with special characters) from file python/pyspark - 从 csv 读取特殊字符并将其写回文件 - python/pyspark - Reading special characters from csv and writing it back to the file Python-从URL参数读取特殊字符 - Python - special characters reading from URL parameter 在python中从网络读取特殊字符 - reading special characters from web in python 从文本文件中删除特殊字符,但保留换行符(Python) - Strip special characters from text file but keep new lines (Python) 如何在python中读取带有特殊字符的文本文件 - How to read a text file with special characters in python 读取带有空格,值,方括号和特殊字符的文本文件 - Reading text file with blanks spaces, values, brackets and special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM