【问题标题】:Upload and read CSV file in google app engine在谷歌应用引擎中上传和读取 CSV 文件
【发布时间】:2017-05-05 11:34:15
【问题描述】:

我正在从表单上传一个 CSV 文件,并希望在 Python 中检索其第一行。我在 App Engine 应用程序上,并使用 webapp2(不知道它是否相关?)。

我尝试了许多在论坛上找到的答案,但没有一个有效。我试过了:

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'

我不理解这些错误,因为其中许多解决方案似乎对其他人有效。

我的表单看起来像这样(我正在使用 Polymer):

<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>

你有什么建议吗? 谢谢!

【问题讨论】:

    标签: python csv upload polymer webapp2


    【解决方案1】:

    我终于找到了问题:它来自Polymer,对象'iron-form'不支持enctype="multipart/form-data"。我删除了is="iron-form",一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 2012-07-27
      • 2014-06-18
      • 1970-01-01
      • 2013-05-19
      相关资源
      最近更新 更多