Home > Software design >  Convert an unknown data item to string in Python
Convert an unknown data item to string in Python

Time:12-03

I have certain data that need to be converted to strings. Example:

[ABCGHDEF-12345, ABCDKJEF-123235,...]

The example above does not represent a constant or a string by itself but is taken from an Excel sheet (ranging upto 30 items for each row). I want to convert these to strings. Since data is undefined, explicitly converting them doesn't work. Is there a way to do this iteratively without placing double/single quotes manually between each data element?

What I want finally:

["ABCGHDEF-12345", "ABCDKJEF-123235",...]

CodePudding user response:

To convert the string to list of strings you can try:

s = "[ABCGHDEF-12345, ABCDKJEF-123235]"

s = s.strip("[]").split(", ")
print(s)

Prints:

['ABCGHDEF-12345', 'ABCDKJEF-123235']
  • Related