English 中文(简体)
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.

最佳回答
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]())
           )
问题回答

I would do this:

temp_list = [c.InnerText for c in row.Descendants[TableCell]()]
temp_list[2] = "Something different"
t = tuple(temp_list)

It would work like this:

>>> temp_list = [i for i in range(4)]
>>> temp_list[2] = "Something different"
>>> t = tuple(temp_list)
>>> t
(0, 1,  Something different , 3)

If every tuple contains 4 elements, then, frankly, I think you d be better off assigning them to individual variables, manipulating those, and then building your tuple:

for row in rows:
    t1, t2, t3, t4 = tuple([c.InnerText for c in row.Descendants[TableCell]()])
    t1 = ...
    t = (t1, t2, t3, t4)

What i ve generally done, but am not a fan of:

l = list(oldtuple) l[2] = foo t = tuple(l)

I kind of want something like update() for dicts

newtuple = update(oldtuple, (None, None, val, None))

Or perhaps the right structure is a zip

newtuple = update(oldtuple, ((2, val), (3, val)))





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签