簡體   English   中英

在Python中解析HTML上傳的CSV文件

[英]Parse HTML uploaded CSV file in Python

我正在使用GAE托管需要從CSV文件輸入內容的網站。 將此csv文件上傳后,我將其轉換為表格。 但是,我遇到了有關Mac和Windows兼容性問題的問題。 無法識別Mac中生成的CSV文件,並且出現錯誤:

new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

這是我的Python代碼

def loop_html(thefile):
    reader = csv.reader(thefile.file)   
    header = reader.next()
    i=1
    iter_html=""
    for row in reader:
        iter_html = iter_html +html_table(row,i)  #generate inputs table
        i=i+1

def html_table(row_inp,iter):
    mai_temp=float(row_inp[0])

    Input_header="""<table border="1">
                        <tr><H3>Batch Calculation of Iteration %s</H3></tr><br>
                        <tr>
                            <td><b>Input Name</b></td>
                            <td><b>Input value</b></td>
                            <td><b>Unit</b></td>
                        </tr>"""%(iter)
    Input_mai="""<tr>
                    <td>Mass of Applied Ingredient Applied to Paddy</td>
                    <td>%s</td>
                    <td>kg</td>
                </tr>""" %(mai_temp) 
    Inout_table = Input_header+Input_mai
    return Inout_table  

后來我將代碼'reader = csv.reader(thefile.file)'更改為'reader = csv.reader(open(thefile.file,'U'))'',給了我不同類型的錯誤:

TypeError: coercing to Unicode: need string or buffer, cStringIO.StringO found

誰能看看我的代碼並給我一些建議? 謝謝!

我剛剛找到了解決方案。 'splitlines()'將處理新行問題。 這是來源

reader = csv.reader(thefile.file.read().splitlines())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM