0

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"
...
11
  • This doesn't look like it has anything to do with encoding. What's in config.ini? Commented Dec 3, 2019 at 23:17
  • @Chris I'll add the config.ini content into the question Commented Dec 3, 2019 at 23:19
  • 1
    myParser.get("Names","firstName").strip('"') Commented Dec 3, 2019 at 23:23
  • 1
    There are quotes in your file. If you don't want them, don't include them. Why are they there? Commented Dec 3, 2019 at 23:25
  • @wpercy that worked, though as the others pointed out since it isn't code, there was no need for the quotes there. I think that I'll take that approach. Thanks for your input Commented Dec 3, 2019 at 23:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.