2017-05-31 124 views
0

我需要從CSV文件輸入數據並創建一個HTML表格作爲輸出。使用Python將CSV文件讀入HTML表格

我目前的工作:

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>') 

CSV文件我想讀入表: https://ufile.io/6joj6

當我運行的功能我得到的錯誤:

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

如果有人提供一些指導或幫助,將不勝感激。

由於提前,

回答

1

錯誤可能是因爲有問題的文件可能無法使用CP1252編碼。假設它使用utf-8編碼,只需在open語句中添加編碼,它就可以工作。我已經測試過它。

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 
+0

當然似乎已經做了些什麼,這是現在的輸出。 http://imgur.com/0qGtzX2 – J22D

+0

你也必須改變你的'定界符'。 –

+0

這似乎只是讀取CSV的最後一行。 – J22D