1

我有一个包含多行的数据列表,我从 Excel 文件中读取了这些数据,然后将其读入列表。现在,我想将此列表打印到另一个文本文件并保存,然后将 html 打印到另一个文件。然后我想制作一个html文件,这样我就可以制作一个表格,让它有4行,每行包含一个数据单元格。

注意:我使用的是 python 3。

到目前为止我已经尝试过了,但它似乎不起作用。

data = ['Course name', 'Course Type', 'Tutor']
       ['English',    , 'Languages' , 'Mr A']
       ['Geography'   , 'Humanities', 'Mr B']
       ['Civil Engineering' , 'Engineering', 'Mr C']

f = open("SomeData.csv", 'w')
for row in mylist:
    print(row,file=f)
4

3 回答 3

3

您的代码列表初始化转换为:

data = ['Course name', 'Course Type', 'Tutor']

['English',    , 'Languages' , 'Mr A']             # Just a statement
['Geography'   , 'Humanities', 'Mr B']             # Just a statement
['Civil Engineering' , 'Engineering', 'Mr C']      # Just a statement

但你需要列出一个列表:

data = [
    ['Course name', 'Course Type', 'Tutor'],
    ['English', 'Languages' , 'Mr A'],
    ['Geography', 'Humanities', 'Mr B'],
    ['Civil Engineering', 'Engineering', 'Mr C']
]

接下来,您可以将数据写入文件:

f = open("output.txt", "w")
for row in data:
    print(", ".join(row), file=f)
f.close()
于 2012-11-25T16:27:05.087 回答
2

按照上述答案中的说明正确组装列表后,您可以按照所述执行输出部分,或者像这样:

f = open("SomeData.csv", 'w')
for row in data:
    f.write(",".join(row)+",")
f.close()

这里是关于以与 CSV 部分相同的方式执行 HTML 输出部分的简单方法的提示。

f = open("output.html", 'w')
f.write("<html><table>")
for row in data:
    f.write("<tr><td>"+"</td><td>".join(row)+"</td></tr>")
f.write("</table></html>")
f.close()

还有一个 python csv 库可以帮助满足更深入的 CSV 需求:http ://docs.python.org/3.3/library/csv.html

于 2012-11-25T16:58:46.463 回答
1
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import csv

class CoolFileWriter:
    """reads a csv file and writes a text or html file"""
    def __init__(self, csv_file, txt_out='out.txt', html_out='out.html'):
        self.csv_file = csv_file
        self.txt_out = txt_out
        self.html_out = html_out
        self.content = []

    def read_csv(self):
        with open(self.csv_file, newline='') as f:
            reader = csv.reader(f)
            for row in reader:
                self.content.append(row)

    def write_txt(self):
        f = open(self.txt_out, 'w')
        for row in self.content:
            f.write(', '.join(row) + '\n')
        f.close()

    def write_html(self):
        html_pre="""
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table Data</title>
<style>
#newspaper-a
{
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    margin: 45px;
    width: 480px;
    text-align: left;
    border-collapse: collapse;
    border: 1px solid #69c;
}
#newspaper-a th
{
    padding: 12px 17px 12px 17px;
    font-weight: normal;
    font-size: 14px;
    color: #039;
    border-bottom: 1px dashed #69c;
}
#newspaper-a td
{
    padding: 7px 17px 7px 17px;
    color: #669;
}
</style>
</head>
<body>
<table id="newspaper-a">
"""
        html_post="""
</table>
</body>
</html>
"""
        f = open(self.html_out, 'w')
        f.write(html_pre)
        th=True
        for row in self.content:
            if (th):
                f.write("<tr>\n\t<th>"+"</th>\n\t<th>".join(row)+"</th>\n</tr>\n")
                th=False
            else:
                f.write("<tr>\n\t<td>"+"</td>\n\t<td>".join(row)+"</td>\n</tr>\n")
        f.write(html_post)
        f.close()

f = CoolFileWriter('excel.csv', 'data.txt', 'data.html')

f.read_csv()
f.write_txt()
f.write_html()

餐桌风格来自这里。:-)

于 2012-11-25T18:55:46.987 回答