0

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.

4
  • 2
    Just do it yourself with 2 for loops Commented Feb 1, 2021 at 16:56
  • Please post what you tried so far Commented Feb 1, 2021 at 21:53
  • Thanks for responses. I've found the prettytable module which is doing what I need. But for now I am facing a trouble with splitting the first string. I need to split it like ['Date', 'Jan 31', 'Jan 30' ... 'Jan 22'] and I could not find a solution yet. Commented Feb 2, 2021 at 7:45
  • It's a pain as you need to split and then join alternately excluding the first date item. I've given you a solution but I am sure there are 100 other maybe better ways of achieving this.. Commented Feb 2, 2021 at 8:58

1 Answer 1

1

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    |
# +------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I need. Thank you very much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.