(I work interactively with a WordprocessingDocument object in IronPython using the OpenXML SDK, but this is really a general Python question that should be applied in all implementations)
I am trying to clear some tables from multiple Word documents. For each table, I have an iterator that gives me table row objects. Then I use the following generator instruction to get tuples of cells from each row:
for row in rows:
t = tuple([c.InnerText for c in row.Descendants[TableCell]()])
Each tuple contains 4 elements. Now, in the column t[1]for each tuple, I need to apply a regular expression to the data. I know that tuples are immutable, so I'm glad to either create a new tuple, or build a tuple in a different way. Given an row.Descendants[TableCell]()iterator returns, what is the most Pythonic (or at least the easiest) way to build a tuple from an iterator, where do I want to change the returned nth element ?
Now my brute force method is to create a tuple from the left slice ( t[:n-1]), the modified data in t[n]and the right slice ( t[n+1:]), but I feel like the module itertoolsshould have something to help me here.
source
share