Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new Tuple with one element modified

(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 nth 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.

like image 386
technomalogical Avatar asked Sep 17 '25 19:09

technomalogical


1 Answers

def item(i, v):
  if i != 1: return v
  return strangestuff(v)

for row in rows:
  t = tuple(item(i, c.InnerText)
            for i, c in enumerate(row.Descendants[TableCell]())
           )
like image 145
Alex Martelli Avatar answered Sep 19 '25 08:09

Alex Martelli