简体   繁体   中英

Python 3.7.4: encoding issues with strings

I have a variable in a python file that contains a string. When I reference this variable in the same file all goes well, though if the values are stored in an .ini file instead, and referenced via ConfigParser.ConfigParser() et al, the string comes back with double quotes around it. It is still of the data type str , though they are no longer the same.

As an example.

firstName = "Eli"
print(firstName) # Eli

myParser = ConfigParser.ConfigParser()
myParser.read("config.ini")

myFirstName = myParser.get("Names","firstName")
print(myFirstName) # "Eli"

the comments above show what the output would be. I'd like to be able to get the latter approach (from the .ini file) to either return the string sans double quotes, or a way to get them out (replace doesn't work). My guess is that it has something to do with encoding, though I cannot seem to figure it out.

Thanks in advance.

Here's what my config.ini file looks like

[Names]
firstName = "Eli"
...

Here is the mistake:

[Names]
firstName = "Eli"
...

You put quotes around the string, therefore you are getting quotes. Config files are plain text, it's not a programming language where you have to differentiate between strings and other data types.

Now I'm not sure why replace doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM