English 中文(简体)
python从html表格中获得档案
原标题:python receive file from html form

我有一份表格,附有投入标记和提交年份:

<input type="file" name="filename" size="25">

我有处理该员额的书记:

def post(self):

Im以表格接收的档案是一份Xml文件,在平线邮局中,我想将这一 foo.xml发送到另一功能,以验证这一文件(使用微型Xsv)。

我的问题是,我如何取回档案? 我尝试:

form = cgi.FieldStorage()

inputfile = form.getvalue( filename )

but this puts the content in the inputfile, I don t have a foo.xml file per se that I can pass to the minisxv function which request a .xml file not the text...

<>Update> 我发现,通过任何途径接受案文而不是投入文件的职能。

最佳回答

Oftentimes, there s also a function to extract XML from a string. For example, minidom has parseString, and lxml etree.XML.

如果你有内容,你可以提出类似档案的物体。 StringIO:

from StringIO import StringIO
content = form.getvalue( filename )
fileh = StringIO(content)
# You can now call fileh.read, or iterate over it

如果您必须在磁盘上存档,则使用tempfile.mkstemp:

import tempfile
content = form.getvalue( filename )
tmpf, tmpfn = tempfile.mkstemp()
tmpf.write(content)
tmpf.close()
# Now, give tmpfn to the function expecting a filename

os.unlink(tmpfn) # Finally, delete the file
问题回答




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

热门标签