Reputation: 75
My result data is in a list of tuples, which each have a list in them:
[(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], ['I', 'J', 'K', 'L', 'M', 'N']),
...
(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], ['I', 'J', 'K', 'L', 'M', 'N'])]
What's the best way to strip all the nesting and quotes and write A:N to a tab delimited file?
Upvotes: 1
Views: 1244
Reputation: 816462
The quotes are not part of the string, they denote the string. You would not be able to remove them.
The csv
module makes this taks pretty straightforward:
import csv, itertools
with open('file.csv', 'wb') as f:
writer = csv.writer(f, delimiter="\t")
writer.writerows(list(itertools.chain(*t)) for t in results)
This results in a file where each row corresponds to a tuple and a row contains the letters of both lists, separated by tabs.
Upvotes: 4
Reputation: 1
Recursive is the natural way to solve this issue.
let target be your list [([A,B..]), ([A, B])]
def dump(target):
for obj in target:
if isinstance(obj,tuple) or isinstance(obj, list):
dump(obj)
else:
print(obj),
dump(target)
print()
Upvotes: -1