Tuple And Csv Reader In Python
Attempting something relatively simple. First, I have dictionary with tuples as keys as follows: (0,1,1,0): 'Index 1' I'm reading in a CSV file which has a corresponding set of fie
Solution 1:
tuple(int(x) for x in ('0','1','1','0')) # returns (0,1,1,0)
So, if your CSV reader object is called csv_reader
, you just need a loop like this:
for row in csv_reader:
tup = tuple(int(x) for x in row)
# ...
Solution 2:
when you read in the CSV file, depending on what libraries you're using, you can specify the delimiter.
typically, the comma is interpreted as the delimiter. perhaps you can specify the delimiter to be something else, e.g. '-', so that set of digits are read together as a string, and you can convert it to a tuple using variety of methods, such as using ast.literal_eval mentioned in converting string to tuple
hope that helps!
Post a Comment for "Tuple And Csv Reader In Python"