Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Handle writing tuples #153
I am working with spatial data and the shape format for a point is an x, y tuple. In the worksheet class, the write method tries all of the options to write this tuple and falls through to the part where it tries a float. With my code I get a
is there a way to successfully store tuples or is it expected that I format them how I want them before writing them to the sheet?
xls = xlsxwriter.Workbook('test.xlsx') book = xls.add_worksheet('test_tuple') book.write_row('A1', [(1, 2)]) xls.close()
I think to be safe I would want the cell to be
Thanks for the example.
In your case if you want a string then you should probably just convert it using
I would probably use a list comprehension:
mylist = [(1, 2)] mylist = [str(t) for t in mylist] # or if there are other data types in the list: mylist = [str(t) if type(t) is tuple else t for t in mylist] worksheet.write_row('A1', mylist)
referenced this issue
Sep 3, 2014
def convert_tuple(value): if not isinstance(value, tuple): return value return str(value)
If you had started with that instead of a stack trace it would have saved some time. :-)
Yes. You are probably right about that.
Looking at the logic of the
This would probably have done the right thing for your case.
On the other hand it might silently work for other cases where people didn't expect it to.
In the end I think that the
In general I don't find stack traces very useful in bug reports (in my own testing yes but not in bug reports). Also in this case the stack trace was from modified code so it is even a little less useful.
The unmodified code raises a
Update: potential fix below.
P.S. I didn't see your tweets until several hours after the open issue. I wasn't ignoring you. :-)
What would you think of something like this:
diff --git a/xlsxwriter/worksheet.py b/xlsxwriter/worksheet.py index e0090bb..e90dda6 100644 --- a/xlsxwriter/worksheet.py +++ b/xlsxwriter/worksheet.py @@ -408,6 +408,8 @@ class Worksheet(xmlwriter.XMLwriter): return self.write_number(row, col, f, *args[1:]) except ValueError: pass + except TypeError: + raise TypeError("Unsupported type %s in write()" % type(token)) # Finally try string. try:
Which for your code example above would give:
$ python ex01.py Traceback (most recent call last): File "ex01.py", line 5, in <module> book.write_row('A1', [(1, 2)]) File "/Users/John/Python/XlsxWriter/xlsxwriter/worksheet.py", line 61, in cell_wrapper return method(self, *args, **kwargs) File "/Users/John/Python/XlsxWriter/xlsxwriter/worksheet.py", line 978, in write_row error = self.write(row, col, token, cell_format) File "/Users/John/Python/XlsxWriter/xlsxwriter/worksheet.py", line 61, in cell_wrapper return method(self, *args, **kwargs) File "/Users/John/Python/XlsxWriter/xlsxwriter/worksheet.py", line 412, in write raise TypeError("Unsupported type %s in write()" % type(token)) TypeError: Unsupported type <type 'tuple'> in write() Exception Exception: Exception('Exception caught in workbook destructor. ...