Grygoriy
Grygoriy

Reputation: 5

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

Can You please suggest me a Python module which will help to convert provided list to a html table? list looks like this:

[' 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  ']

Thanks in advance.

Upvotes: 0

Views: 250

Answers (1)

Johnny John Boy
Johnny John Boy

Reputation: 3283

The data is a little messy and this could definitely do with cleaning up to be more efficient but as a hacky way of getting what you need this should do the trick:

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

Upvotes: 1

Related Questions