python - 在Python中解析HTML上传的CSV文件

标签 python csv html-parsing

我正在使用 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()' 将处理新行问题。这是source .

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

关于python - 在Python中解析HTML上传的CSV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14651726/

相关文章:

python - 在 pandas.drop_duplicates 之后重建索引

python - 在 Python 中将嵌套的 JSON 转换为 CSV 文件

php - 从 HTML 中读取属性值

python - 从 HTMLparser 获取 HTML 子树

python - pudb 调试器可以在 windows 上使用吗?

python - 使用 Python 渲染 TTF 字形

python - 在轴上放置文本值而不是数值

ruby-on-rails - 从 CSV 导入和创建记录

bash - 如何使用 shell 脚本有效地处理大型 csv 文件,以获得比以下脚本更好的性能?

c# - 如何使用 C# 从 DIV 中抓取图像