English 中文(简体)
将装上载的Csv改为Python清单
原标题:converting an uploaded csv to python list

我有一个双栏杆,我通过一个超文本页面上载,用一只字眼书写。 查阅服务器方面的档案,看来是一个很长的路要道,即用内容标注的文档。

col1,  col2  
x,y  

工作已经结束。

( upfile ,  test.csv ,  col1,col2 	
x,y ) 

Col1 contains the data I want to operate on (i.e. x) and col 2 contains its identifier (y). Is there a better way of doing the uploading or do I need to manually extract the fields I want - this seems potentially very error-prone thanks

问题回答

如果您重新使用<代码>cgi 在座标上,你应该能够做以下事情:

form = cgi.FieldStorage()
thefile = form[ upfile ]

reader = csv.reader(thefile.file)
header = reader.next() # list of column names
for row in reader:
    # row is a list of fields
    process_row(row)

See, for example, cgi program or the python cgi模块 docs.

你们能否利用钟式模块来进行这种教育? 这比你自己推出的情况好得多。

大致如下:

import csv
import cgi

form = cgi.FieldStorage()
thefile = form[ upfile ]

reader = csv.reader(thefile, delimiter= , )
for row in reader:
  for field in row:
    doThing()

www.un.org/Depts/DGACM/index_spanish.htm 更正我的回答如下。

Looks like your file is becoming modified by the HTML upload. Is there anything stopping you from just ftp ing in and dropping the csv file where you need it?

一旦CSV档案更加恰当,就是一个快速功能,将它纳入2D阵列:

def genTableFrCsv(incsv):
    table = []
    fin = open(incsv,  rb )
    reader = csv.reader(fin)
    for row in reader: 
        table.append(row)
    fin.close()
    return table

从这里看,你可以在整个名单上进行记忆,而不是从档案中抽取比照,就像在维德尔解决方案中那样。

The easy solution is rows = [row.split( ) for r in csv_string.split( )]. It s only error proned if you have users from different platforms submit data. They might submit comas or tabs and their line breaks could be , , , or ^M. The easiest solution is to use regular expressions. Book mark this page if you don t know regular expressions:

rel=“nofollow”>http://regexlib.com/CheatSheet.aspx

这里的解决办法是:

import re

csv_string =  col1,col2 	
x,y  #obviously your csv opening code goes here

rows = re.findall(r (.*?)[	,](.*?) ,csv_string)
rows = rows[1:] # remove header

现在,罗子是所有各行的les子清单。





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

热门标签