[英]Read CSV file into HTML tables with Python
I'm needing to input data from a CSV file and create a HTML table as the output.我需要从 CSV 文件输入数据并创建一个 HTML 表作为输出。
I am currently working with:我目前正在与:
with open('2016motogp.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter='\t')
for row in reader:
print('<tr>')
for fn in reader.fieldnames:
print('<td>{}</td>'.format(row[fn]))
print('</tr>')
The CSV file I want to read into the table is: https://ufile.io/6joj6我想读入表格的 CSV 文件是: https : //ufile.io/6joj6
When I run the function I get the error:当我运行该函数时,出现错误:
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-11-3a27549e50fe> in <module>()
----> 1 write_html_table("2016motogp")
<ipython-input-9-91d2a78b30ad> in write_html_table(filename)
55 with open(filename + ".csv") as csvfile:
56 reader = csv.DictReader(csvfile, delimiter='\t')
---> 57 for row in reader:
58 print('<tr>')
59 for fn in reader.fieldnames:
E:\Anaconda\lib\csv.py in __next__(self)
109 if self.line_num == 0:
110 # Used only for its side effect.
--> 111 self.fieldnames
112 row = next(self.reader)
113 self.line_num = self.reader.line_num
E:\Anaconda\lib\csv.py in fieldnames(self)
96 if self._fieldnames is None:
97 try:
---> 98 self._fieldnames = next(self.reader)
99 except StopIteration:
100 pass
E:\Anaconda\lib\encodings\cp1252.py in decode(self, input, final)
21 class IncrementalDecoder(codecs.IncrementalDecoder):
22 def decode(self, input, final=False):
---> 23 return codecs.charmap_decode(input,self.errors,decoding_table)[0]
24
25 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1037: character maps to <undefined>
If anybody provide some guidance or help out it would be much appreciated.如果有人提供一些指导或帮助,将不胜感激。
Thanks in advance,提前致谢,
Use Python pandas DataFrame to_html method使用 Python pandas DataFrame to_html 方法
You could read the csv file into a Python pandas DataFrame.您可以将 csv 文件读入 Python pandas DataFrame。 Then, use the DataFrame to_html
feature to either create an HTML file or assign the result into a string and use it that way.然后,使用数据帧to_html
功能可以创建一个HTML文件或结果分配到一个字符串,并使用它的方式。 This converts the DataFrame into an HTML table.这会将 DataFrame 转换为 HTML 表格。 See links to Python docs below.请参阅下面的 Python 文档链接。
import pandas as pd
# Read the csv file in
df = pd.read_csv('2016motogp.csv')
# Save to file
df.to_html('myTable.htm')
# Assign to string
htmTable = df.to_html()
The error might be because the file in question might not be using CP1252 encoding.该错误可能是因为相关文件可能未使用 CP1252 编码。 Assuming it's using utf-8
encoding just add the encoding in open
statement and it'll work.假设它使用utf-8
编码,只需在open
语句中添加编码即可。 I have tested it.我已经测试过了。
import csv
table = ''
with open(csv_path, encoding="utf8") as csvFile:
reader = csv.DictReader(csvFile, delimiter=',')
table = '<tr>{}</tr>'.format(''.join(['<td>{}</td>'.format(header) for header in reader.fieldnames]))
for row in reader:
table_row = '<tr>'
for fn in reader.fieldnames:
table_row += '<td>{}</td>'.format(row[fn])
table_row += '</tr>'
table += table_row
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.