In Python how to convert a string with special characters to a tuple
Question:
would really appreciate any help on this one!
I have a string:
"<Cell 'test'.B45>"
I need to convert this to a tuple with only one object in it, so that looks like this:
((<Cell 'test'.B45>))
So to do this I have tried:
string = "<Cell 'test'.B45>"
tuple = eval(string)
this returns:
SyntaxError: invalid syntax
I think that this is because of the special values ("<",">", "’") in the string (removing them seems to work but I need them here)
Am I right here? and is there a way round this?
note I have also tried using the method:
tuple = ast.literal_eval(string)
with the same result.
Answers:
Is that what you’re saying?
string = "<Cell 'test'.B45>"
list_of_str = list()
list_of_str.append(string)
tuple_info = tuple(list_of_str)
print(tuple_info)
# ("<Cell 'test'.B45>",)
would really appreciate any help on this one!
I have a string:
"<Cell 'test'.B45>"
I need to convert this to a tuple with only one object in it, so that looks like this:
((<Cell 'test'.B45>))
So to do this I have tried:
string = "<Cell 'test'.B45>"
tuple = eval(string)
this returns:
SyntaxError: invalid syntax
I think that this is because of the special values ("<",">", "’") in the string (removing them seems to work but I need them here)
Am I right here? and is there a way round this?
note I have also tried using the method:
tuple = ast.literal_eval(string)
with the same result.
Is that what you’re saying?
string = "<Cell 'test'.B45>"
list_of_str = list()
list_of_str.append(string)
tuple_info = tuple(list_of_str)
print(tuple_info)
# ("<Cell 'test'.B45>",)