English 中文(简体)
存储循环迭代数据
原标题:storing for loop iteration data

我正在玩cgi(上传文件格式),

我正在接收作为存储对象的文件,并将其保存在(输入)变量中。

这是简单的迭代。

for file in input:
  filepath = ....
  filename, fileext = os.path.splitext(filepath)
  file_real_name = ....
  file_size = ....
  file_type = ...
  file_url = ....
  file_short_name = ...
  file_show_link = ....

  # etc

如果只有一个文件会很容易,但如果我有多个文件呢?

我怎样才能有另一个值来保存中的所有迭代信息

比如uploaded_files,在那里我可以访问每个上传的文件以及上述迭代的所有信息?

我试图阅读文档,但我还不能理解一些迭代概念,抱歉:)

最佳回答

您希望使用数据结构来保存数据。根据复杂性,您可能只需要使用一系列字典:

files = []
for file in input:
    files.append({
        "path": get_path(file),
        "name": get_name(file),
        "size": get_size(file),
        ...
    })

或者,如果你发现需要对数据执行大量操作,你可能想创建自己的类并创建一个对象列表:

class SomeFile:
    def __init__(self, path, name, size, ...):
        self.path = path
        ...

    def do_something_with_file(self):
        ...

files = []
for file in input:
    files.append(SomeFile(get_path(file), get_name(file), get_size(file), ...))

请注意,这里您遵循的是通过迭代迭代器来构建列表的模式。您可以使用a列出理解,例如:

[{"path": get_path(file), "name": get_name(file), ...} for file in input]

还要注意,文件输入文件()input()

问题回答
results = []
for i in range(5):
    file_data = {}
    file_data[ a ] = i
    file_data[ b ] = i**2
    results.append(file_data)
print results




相关问题
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 ]="...