3

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?

psr
  • 2,619
  • 4
  • 32
  • 57
  • This is going to sound random but can you try `id_of_first_link = id_of_first_link.replace('%', '%%')` before putting it into the dictionary, and see what happens? – Vasili Syrakis Jun 22 '16 at 12:41
  • It works! But there is no change in the string. Why is it so? Here is the new string after the replace function: `http://www.ft.com/cms/s/0/46858280-36f4-11e6-a780-b48ed7b6126f.html?ftcamp=published_links%2Frss%2Fcompanies_financial-services%2Ffeed%2F%2Fproduct` – psr Jun 22 '16 at 12:45
  • The percent signs have special meaning and putting them twice seems to allow to escape them. – zezollo Jun 22 '16 at 12:48

1 Answers1

2

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.

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
  • Works perfect! Thanks. – psr Jun 22 '16 at 12:53
  • I am getting a problem in converting the `id_of_first_link` to a raw string, since the program is directly storing the value `http://www.ft.com/cms/s/0/46858280-36f4-11e6-a780-b48ed7b6126f.html?ftcamp=published_links%2Frss%2Fcompanies_financial-services%2Ffeed%2F%2Fproduct` in the `id_of_first_link`, I am unable to find a way to convert this to raw string. I tried `id_of_first_link.encode('string-escape')`, but it gives me error `LookupError: unknown encoding: string-escape` – psr Jun 24 '16 at 08:16