(I am working interactively with a WordprocessingDocument object in IronPython using the OpenXML SDK, but this is really a general Python question that should be applicable across all implementations)
I am trying to scrape out some tables from a number of Word documents. For each table, I have an iterator that is giving me table row objects. I then use the following generator statement to get a tuple 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 column t[1]
for each tuple, I need to apply a regex to the data. I know that tuples are immutable, so I m happy to either create a new tuple, or build the tuple in a different way. Given that row.Descendants[TableCell]()
returns an iterator, what s the most Pythonic (or at least simplest) way to construct a tuple from an iterator where I want to modify the n
th element returned?
My brute-force method right now 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 itertools
module should have something to help me out here.