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