Reputation: 4571
I'm retrieve some data from a web API, but the retrieved records comes with extra quotes I need to remove; result = ['"A1","Location1"', '"A2","Location2"']
What is the most efficient way to do this for a large datasets, say 10000 items so that I end up with something like this:
result = ["A1","Location1", "A2","Location2"]
Upvotes: 0
Views: 104
Reputation: 20583
Another not great (little dirty) but working solution is to use ast.literal_eval
with repr
:
from ast import literal_eval
result = ['"A1","Location1"', '"A2","Location2"']
literal_eval(repr(result).replace("\'", ""))
['A1', 'Location1', 'A2', 'Location2']
Upvotes: 0
Reputation: 882691
If each and every item of list result
is a comma-separated string of "quoted" sub-items (not containing commas within a sub-item), you can easily make a new list clean
as follows:
clean = []
for item in result:
subitems = item.split(',')
clean.extend(si[1:-1] for si in subitems)
If your constraints are more complicated (quotes may or may not be there around eadh sub-item, a quoted sub-item may contain commas, etc, etc -- I can't just blindly guess right on every detailed constrain you didn't let us know about!-) the task will be proportionately harder, perhaps involving regular expressions, or worse... but we won't know until and unless you edit your Q to specify very precisely what, exactly, you have to deal with!-)
Upvotes: 7
Reputation: 314
Use python's built in CSV module. It does this automatically.
Upvotes: -1