Prabhjot Singh Rai
Prabhjot Singh Rai

Reputation: 2515

ValueError in ConfigParser in python3

I am trying to update my configuration.ini file's, [Financial Times: Financial-Services] section's last_entry_id with the following string:

http://www.ft.com/cms/s/0/46858280-36f4-11e6-a780-b48ed7b6126f.html?ftcamp=published_links%2Frss%2Fcompanies_financial-services%2Ffeed%2F%2Fproduct

Here is my configuration.ini file's corresponding section:

[Financial Times: Financial-Services]
feed_updated_on = 
feed_updated_field = ['updated']
link = http://www.ft.com/rss/companies/financial-services
last_entry_id = 
id_field = link

But in my code, when I try the following line:

id_of_first_link = 'http://www.ft.com/cms/s/0/46858280-36f4-11e6-a780-b48ed7b6126f.html?ftcamp=published_links%2Frss%2Fcompanies_financial-services%2Ffeed%2F%2Fproduct'
feed_link['last_entry_id'] = id_of_first_link

I get the error:

ValueError: invalid interpolation syntax in 'http://www.ft.com/cms/s/0/46858280-36f4-11e6-a780-b48ed7b6126f.html?ftcamp=published_links%2Frss%2Fcompanies_financial-services%2Ffeed%2F%2Fproduct' at position 90

feed_link is the reference name of [Financial Times: Financial-Services] in the configuration.ini file. How to resolve this issue?

Upvotes: 3

Views: 3348

Answers (1)

Vasili Syrakis
Vasili Syrakis

Reputation: 9591

The exception refers to an invalid interpolation syntax

This is because in Python you're able to interpolate strings with the following syntax:

mystring = "Hello, my name is %s"
print(mystring % 'StackOverflow')

I think that at some point in configparser, it must be using this syntax, and therefore is getting a string that it cannot handle correctly.

There are a few ways to get around this. You can either use the str.replace() that I showed you, which escapes % by using a double percent sign %%...

Or you can try to provide a raw string instead, with this syntax:

id_of_first_link = r'http://www.ft.com/cms/s/0/46858280-36f4-11e6-a780-b48ed7b6126f.html?ftcamp=published_links%2Frss%2Fcompanies_financial-services%2Ffeed%2F%2Fproduct'

Note the r before the starting single-quote.

Raw strings will also allow you to type strings like r'C:\Windows\system32' without the backslashes escaping characters accidently.

Upvotes: 2

Related Questions