0

I am having a use case where I need to pass a value which is having semi colons and colons for a key in a python dictionary. But it gives me a syntax error. Seems like this does not happen if the value is enclosed in double quotes instead of conventional single quote. Is there a way to impose that? Or, some other work around?

Here is the error -

props = {'creds1':'UserListings;service;SomeValue;Username:Mohan;Password:passkey123','creds2':''UserListings2;service;SomeValue2;Sample:Sikander;Password:passkey123'}

File "", line 1 monProps = {'creds1':'UserListings;service;SomeValue;Username:Mohan;Password:passkey123','creds2':''UserListings2;service;SomeValue2;Sample:Sikander;Password:passkey123'} ^ SyntaxError: invalid syntax

Thanks in advance!

coder
  • 129
  • 1
  • 1
  • 10

2 Answers2

0

use escape for special characters:

props = {'creds1':'UserListings;service;SomeValue;Username:Mohan;Password:passkey123','creds2':'\'UserListings2;service;SomeValue2;Sample:Sikander;Password:passkey123'}
print(props)

output:

{'creds1': 'UserListings;service;SomeValue;Username:Mohan;Password:passkey123', 'creds2': "'UserListings2;service;SomeValue2;Sample:Sikander;Password:passkey123"}
riccardo nizzolo
  • 601
  • 1
  • 7
  • 21
0

This works fine for me:

props = {'creds1': 'UserListings;service;SomeValue;Username:Mohan;Password:passkey123',
         'creds2': 'UserListings2;service;SomeValue2;Sample:Sikander;Password:passkey123'}

Or, if you genuinely have a quote in your text, you can escape as so:

props = {'creds1': 'UserListings;service;SomeValue;Username:Mohan;Password:passkey123',
         'creds2': '\'UserListings2;service;SomeValue2;Sample:Sikander;Password:passkey123'}

Alternatively, to use just single quotes within strings, as you mention double quotes will work:

props = {"creds1": "UserListings;service;SomeValue;Username:Mohan;Password:passkey123",
         "creds2": "'UserListings2;service;SomeValue2;Sample:Sikander;Password:passkey123"}
jpp
  • 159,742
  • 34
  • 281
  • 339