簡體   English   中英

Python:如何從字符串列表創建 html 表

[英]Python: How to create an html table from a list of strings

您能否向我推薦一個 Python 模塊,這將有助於將提供的列表轉換為 html 表? 列表如下所示:

[' Date Jan 31 Jan 30 Jan 29 Jan 28 Jan 27 Jan 26 Jan 25 Jan 24 Jan 23 Jan 22 ',
 ' ID1  6   7   6   7   7   7   5   7   7   7  ',
 ' ID2  6   6   4   6   7   5   6   7   5   6  ',
 ' ID3  6   7   6   5   6   7   6   2   7   5  ',
 ' ID4  4   5   3   6   7   5   7   7   7   5  ',
 ' ID5  0   0   0   0   0   0   0   0   0   0  ',
 ' ID6  0   0   0   0   0   0   0   0   0   0  ',
 ' ID7  0   0   0   0   0   0   0   0   0   0  ']

提前致謝。

數據有點混亂,這肯定可以通過清理來提高效率,但作為一種獲取所需內容的 hacky 方式,這應該可以解決問題:

from prettytable import PrettyTable
x = PrettyTable()

data = [' Date Jan 31 Jan 30 Jan 29 Jan 28 Jan 27 Jan 26 Jan 25 Jan 24 Jan 23 Jan 22 ', ' ID1 6 7 6 7 7 7 5 7 7 7 ', ' ID2 6 6 4 6 7 5 6 7 5 6 ', ' ID3 6 7 6 5 6 7 6 2 7 5 ', ' ID4 4 5 3 6 7 5 7 7 7 5 ', ' ID5 0 0 0 0 0 0 0 0 0 0 ', ' ID6 0 0 0 0 0 0 0 0 0 0 ', ' ID7 0 0 0 0 0 0 0 0 0 0 ']

clean_data = []
header_list = []
for row in data:
    clean_data.append(row.strip().split()) # iterate through original list and remove spaces from outer items then split by spaces

data_to_merge = clean_data[0] # get first row from clean list
data_to_merge = data_to_merge[1:] # remove date from list
alternate_join = map(' '.join, zip(data_to_merge[::2], data_to_merge[1::2])) # join alternately to get Jan 31, Jan 30 etc rather than Jan, 31, Jan, 30
converted_to_list = list(alternate_join) # convert map to list

converted_to_list.insert(0, clean_data[0][0]) # put the date header item back in the beginning of the list

x.field_names = converted_to_list # add the field names to PrettyTable
x.add_rows(clean_data[1:]) # add the rows to PrettyTable
print(x) # Done!

# +------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
# | Date | Jan 31 | Jan 30 | Jan 29 | Jan 28 | Jan 27 | Jan 26 | Jan 25 | Jan 24 | Jan 23 | Jan 22 |
# +------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
# | ID1  |   6    |   7    |   6    |   7    |   7    |   7    |   5    |   7    |   7    |   7    |
# | ID2  |   6    |   6    |   4    |   6    |   7    |   5    |   6    |   7    |   5    |   6    |
# | ID3  |   6    |   7    |   6    |   5    |   6    |   7    |   6    |   2    |   7    |   5    |
# | ID4  |   4    |   5    |   3    |   6    |   7    |   5    |   7    |   7    |   7    |   5    |
# | ID5  |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |
# | ID6  |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |
# | ID7  |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |   0    |
# +------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM