【问题标题】:How To Read and Print Data from CSV file into HTML File as Text如何将 CSV 文件中的数据作为文本读取并打印到 HTML 文件中
【发布时间】:2022-04-13 16:30:53
【问题描述】:

我一直在尝试从 .csv 文件中读取数据并将数据作为文本/数字打印到 HTML 文件中。像下面的例子:

现在,我想用这样的 HTML 打印:

先生的当前年龄。 abc30 年。粗体和斜体文本/数字将来自 csv 文件,一旦 csv 文件更新,HTML 中的数据也会更新。 csv 和 HTML 文件都将在同一个本地文件夹中。

【问题讨论】:

标签: javascript python html csv


【解决方案1】:

通常的方法是使用 Python 内置的 CSV 阅读器将每一行正确解析为值列表。可以这样做:

import csv

with open('input.csv') as f_input, open('output.html', 'w') as f_output:
    csv_input = csv.reader(f_input)
    header = next(csv_input)    # skip the header
    
    # Write the HTML header
    f_output.write("<html>\n<body>\n")
    
    for row in csv_input:
        f_output.write(f"Current age of Mr. {row[0]} is {row[1]} Years.<br>\n")
        
    # Write the HTML footer
    f_output.write("</body>\n</html>\n")

对于您的示例 CSV 数据,这将产生以下 HTML 输出:

<html>
<body>
Current age of Mr. abc is 20 Years.<br>
Current age of Mr. xyz is 30 Years.<br>
Current age of Mr. jkl is 40 Years.<br>
</body>
</html>

要为每行创建一个 HTML,您需要重新安排一些内容,并确定输出文件的命名方案。这会将名称添加到每个文件名:

import csv

with open('input.csv', encoding='utf-8') as f_input:
    csv_input = csv.reader(f_input)
    header = next(csv_input)    # skip the header

    for row in csv_input:
        with open(f'output_{row[0]}.html', 'w', encoding='utf-8') as f_output:
            # Write the HTML header
            f_output.write("<html>\n<body>\n")

            f_output.write(f"Current age of Mr. {row[0]} is {row[1]} Years.<br>\n")
        
            # Write the HTML footer
            f_output.write("</body>\n</html>\n")

另一种方法是自己解析每一行,可以按如下方式完成:

with open('input.csv') as f_input, open('output.html', 'w') as f_output:
    header = next(f_input)    # skip the header
    
    # Write the HTML header
    f_output.write("<html>\n<body>\n")
    
    for line in f_input:
        row = line.strip().split(',')
        f_output.write(f"Current age of Mr. {row[0]} is {row[1]} Years.<br>\n")
        
    # Write the HTML footer
    f_output.write("</body>\n</html>\n")
   

如果您的单个名称列包含逗号,这将不起作用。例如"John, Smith"(在CSV文件中有效,引号用来忽略里面的逗号)

【讨论】:

  • 这很好用,谢谢。请问可以修改为每个 CSV 行创建一个单独的 HTML 输出文件吗?
  • 你只需要移动循环所在的位置。我添加了一个例子
【解决方案2】:

CSV数据的链接无效,所以很难说,但总的来说:

  • 读取 CSV 文件并将行拆分为特定的变量(查看如何读取文件、如何拆分 css 以及分配到变量中)
  • 您可以在 HTML 字符串中使用变量,您可以使用 Jinja 模板,但我相信手动格式化字符串就可以了。

例如,如果数据文件是data.csv 并且看起来像这样:

John:30
Emily:40
Carla:25

您可以使用with open 例程阅读它:

with open('data.csv', 'r') as datafile:
    data = datafile.readlines()

现在您将所有数据都放在一个行列表中,因此您可以遍历列表并解析数据:

newdata = []
for line in data:
    newdata.append(line.rstrip().split(':'))

现在您应该拥有 newdata 列表中的所有对联,您可以随意使用它。例如要打印出 HTML 表格,您可以这样做:

print('<table>')
for couple in newdata:
    print(f"<tr><td>{couple[0]}</td><td>{couple[1]}</td></tr>")
print('</table>')

这将打印出表格的 HTML 代码。为了能够通过 CSS 控制某些元素的外观,您需要在某些类中使用 divspan 之类的标签,如下所示:

for couple in newdata:
    print(f"The age of <div class="name">{couple[0]}</div> is <div class="age">{couple[1]}</div>.")

如前所述,当解析数据时,您可以使用任何生成 HTML 的技术——手册、Jinja 模板……来保存 HTML 文件。

【讨论】:

  • 这个解决方案也很好用,谢谢!我必须添加encoding="utf-8",因为我的 CSV 是多语言的并且出现了编码错误。您能否添加一些关于输出到一个或多个 HTML 文件的示例?
【解决方案3】:

您的链接数据文件不是有效链接。 我仍然会为您提供一个示例实现,您可以根据自己的情况进行调整:

sameple.csv

Name, Age
ABC, 30
...

在同一文件夹中有一个单独的脚本来根据您的 csv 创建 html

csv_data=open('sample.csv','r').read().split('\n')
html_body = ''

for line in csv_data:
    line = line.split(',')
    html_body += f'<p>Mr. {line[0]} is {line[1]} Years</p>\n'

html_output = '<html><body>\n'+html_body+'</body></html>'
with open('html_out.html', 'w') as outfile:
outfile.write(html_output)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2020-10-19
    • 1970-01-01
    • 2019-03-24
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多