2017-05-05 112 views
0

我從表單上傳CSV文件,並希望在Python中檢索其第一行。我在App Engine應用程序上,並使用webapp2(不知道它是否相關?)。在谷歌應用程序引擎中上傳和讀取CSV文件

我嘗試了很多在論壇上找到的答案,但都沒有工作。我想:

def post(self): 
    csv_file = self.request.POST.get('csvFile') 
    fileReader = csv.reader(csv_file.file) 

,但我得到:AttributeError: 'unicode' object has no attribute 'file'

我也試過:

def post(self): 
    csvFile = self.request.get('csvFile') 
    print csvFile 

    stringReader = csv.reader(StringIO.StringIO(csvFile)) 
    for row in stringReader: 
     print row 

,但我得到:

C:\fakepath\my_file.csv ['C:\\fakepath\\my_file.csv']

爲我打印語句(無論是我做request.get或request.POST.get)。

我也試過:

def post(self): 
    content = self.request.POST.multi['csvFile'].file.read() 

,但我得到:AttributeError: 'str' object has no attribute 'file'

我也試過:

def post(self): 
    csvFile = self.request.files['csvFile'] 
    file = open(csvFile, 'r') 

,但我得到:AttributeError: files

我也試過:

def post(self): 
    csvFile = self.request.POST['csvFile'].value.decode('utf-8') 
    file = csvFile.splitlines() 
    data = csv.DictReader(file) 

,但我得到:AttributeError: 'unicode' object has no attribute 'value'

因爲許多這些解決方案似乎爲其他人工作,我不明白這些錯誤。

我的形式看起來像這樣(我用聚合物):

<form is="iron-form" action="/upload" method="POST" enctype="multipart/form-data" 
          on-iron-form-submit="_formSubmitted" id="form" name="form"> 

         <div class="box"> 
          <input type="file" name="csvFile" id="file" class="inputfile" 
            data-multiple-caption="{count} files selected" multiple on-tap="_chooseFile"/> 
          <label for="file" name="label"> 
           <span>Choose a CSV file...</span></label> 

         </div> 

你有什麼建議? 謝謝!

回答

0

我終於發現問題:它來自Polymer,對象'鐵形式'不支持enctype="multipart/form-data"。我刪除了is="iron-form"和所有工作正常。

相關問題