Интернет скопировал и получил несколько списков, но они не разделены запятыми. Как превратить их в стол? - PullRequest
0 голосов
/ 19 июня 2020

У меня есть несколько списков с веб-сайта, но я не знаю и не могу узнать, как превратить их в таблицу, потому что они не разделены запятыми. Я пытался, но безуспешно, так что, возможно, вы сможете помочь. Я использовал следующий код:

import urllib.request
import pandas as pd
from tabulate import tabulate
source = urllib.request.urlopen('https://de.statista.com/statistik/daten/studie/185581/umfrage/mittlere-kronenverlichtung-in-deutschland-seit-1984/').read()
soup = bs.BeautifulSoup(source,'lxml')
table = soup.find('table')
table_rows = table.find_all('tr')
for tr in table_rows:
    td = tr.find_all('td')
    row = [i.text for i in td]
    print(row)```

*The result is as followed:  
['2019', '25,1%', '23,9%', '22,4%', '28,6%', '28,2%', '23,3%', '26,9%']  
['2018', '22%', '21,5%', '18,3%', '25,1%', '25,7%', '19,8%', '24,9%']  
['2017', '19,7%', '19,7%', '17,4%', '22,5%', '22,9%', '17,6%', '19,5%']  
['2016', '21,2%', '21%', '17,5%', '28,6%', '21,4%', '18,4%', '20,4%']  
['2015', '20%', '20,6%', '16,9%', '23,3%', '24,1%', '17,7%', '19,2%']  
['2014', '20,4%', '20,2%', '16,4%', '27,6%', '24,7%', '17,7%', '17,8%']  
['2013', '19,3%', '18,8%', '15,1%', '23,6%', '27%', '17,3%', '15,6%']  
['2012', '19,2%', '19,3%', '14,5%', '24,3%', '29,4%', '18,7%', '15,7%']  
['2011', '20,4%', '19,1%', '15,6%', '30,4%', '26,3%', '18,8%', '16,7%']  
['2010', '19,1%', '18,7%', '16%', '23,3%', '29,6%', '17,6%', '15,5%']  
['2009', '19,7%', '19,4%', '15,8%', '27%', '26,5%', '19,7%', '14,9%']  
['2008', '20,4%', '20,8%', '18,9%', '22%', '28,3%', '22,2%', '16,5%']  
['2007', '20,7%', '20,8%', '17,8%', '25,6%', '28%', '20,4%', '17,8%']  
['2006', '21%', '19,7%', '18,7%', '27,7%', '26,6%', '19,9%', '18,2%']  
['2005', '21,5%', '21,8%', '18,6%', '27%', '28,1%', '19,8%', '18,2%']  
['2004', '22,8%', '23,6%', '18,5%', '30,5%', '28,5%', '21%', '19,7%']  
['2003', '19,9%', '20,8%', '17,5%', '22,7%', '25,4%', '19,9%', '17,6%']  
['2002', '19,1%', '20,2%', '16,9%', '22,3%', '22,5%', '18,9%', '15,8%']  
['2001', '18,8%', '20,1%', '16,4%', '22,8%', '24%', '18,1%', '13,5%']  
['2000', '19,3%', '19,7%', '16,6%', '25,6%', '24,4%', '18,7%', '14,5%']  
['1999', '18,6%', '19%', '15,9%', '23,2%', '26,2%', '18,4%', '14,7%']  
['1998', '18,3%', '19,4%', '15%', '22%', '24,9%', '18,8%', '15,1%']  
['1997', '18,8%', '18,7%', '16,2%', '22,7%', '28,2%', '18,8%', '15,8%']  
['1996', '18,4%', '17,8%', '15,8%', '22%', '28%', '20,3%', '16,1%']  
['1995', '19,2%', '19,1%', '16,6%', '23,9%', '25%', '21,3%', '16,2%']  
['1994', '20,4%', '20,6%', '19%', '21,7%', '26,7%', '22%', '17,5%']  
['1993', '19,7%', '20%', '17%', '22,9%', '25,4%', '21,8%', '17,5%']  
['1992', '21,2%', '20,8%', '19,7%', '24,8%', '22,8%', '20,6%', '21,4%']  
['1991', '21,1%', '19,9%', '22,8%', '20,7%', '23,4%', '20,4%', '19%']  
['1990', '18,3%', '18,1%', '17,6%', '20,3%', '19,8%', '20,1%', '16,1%']  
['1989', '17,2%', '17,6%', '16,1%', '17%', '20,9%', '19,5%', '13,3%']  
['1988', '16,8%', '16,9%', '16,6%', '17,2%', '18,8%', '19,6%', '12%']  
['1987', '17,7%', '17,2%', '17,2%', '20,1%', '19,2%', '21,7%', '12,1%']  
['1986', '18,1%', '19,7%', '16,6%', '16,6%', '19,2%', '25,2%', '11,9%']  
['1985', '17,7%', '20%', '16,5%', '15,2%', '17,5%', '24,3%', '10,3%']  
['1984', '18,9%', '21,3%', '18%', '17%', '15,9%', '22,2%', '9,9%']*

*Now I would like to turn those results into a table with 8 columns. Also i would like to add headings above every single column. The first column are the years and the other ones values for different things.  
Thank you very much in advance for your help.*

Ответы [ 2 ]

0 голосов
/ 19 июня 2020

Вы можете использовать модуль, pandas:

import urllib.request
import pandas as pd
from tabulate import tabulate
from pandas import DataFrame

source = urllib.request.urlopen('https://de.statista.com/statistik/daten/studie/185581/umfrage/mittlere-kronenverlichtung-in-deutschland-seit-1984/').read()
soup = bs.BeautifulSoup(source,'lxml')
table = soup.find('table')
table_rows = table.find_all('tr')

l = []
for tr in table_rows:
    td = tr.find_all('td')
    row = [i.text for i in td]
    l.append(row)

df = DataFrame(l)

print(df.to_string(index=False))

Вывод:

    0      1      2      3      4      5      6      7
 2019  25,1%  23,9%  22,4%  28,6%  28,2%  23,3%  26,9%
 2018    22%  21,5%  18,3%  25,1%  25,7%  19,8%  24,9%
 2017  19,7%  19,7%  17,4%  22,5%  22,9%  17,6%  19,5%
 2016  21,2%    21%  17,5%  28,6%  21,4%  18,4%  20,4%
 2015    20%  20,6%  16,9%  23,3%  24,1%  17,7%  19,2%
 2014  20,4%  20,2%  16,4%  27,6%  24,7%  17,7%  17,8%
 2013  19,3%  18,8%  15,1%  23,6%    27%  17,3%  15,6%
 2012  19,2%  19,3%  14,5%  24,3%  29,4%  18,7%  15,7%
 2011  20,4%  19,1%  15,6%  30,4%  26,3%  18,8%  16,7%
 2010  19,1%  18,7%    16%  23,3%  29,6%  17,6%  15,5%
 2009  19,7%  19,4%  15,8%    27%  26,5%  19,7%  14,9%
 2008  20,4%  20,8%  18,9%    22%  28,3%  22,2%  16,5%
 2007  20,7%  20,8%  17,8%  25,6%    28%  20,4%  17,8%
 2006    21%  19,7%  18,7%  27,7%  26,6%  19,9%  18,2%
 2005  21,5%  21,8%  18,6%    27%  28,1%  19,8%  18,2%
 2004  22,8%  23,6%  18,5%  30,5%  28,5%    21%  19,7%
 2003  19,9%  20,8%  17,5%  22,7%  25,4%  19,9%  17,6%
 2002  19,1%  20,2%  16,9%  22,3%  22,5%  18,9%  15,8%
 2001  18,8%  20,1%  16,4%  22,8%    24%  18,1%  13,5%
 2000  19,3%  19,7%  16,6%  25,6%  24,4%  18,7%  14,5%
 1999  18,6%    19%  15,9%  23,2%  26,2%  18,4%  14,7%
 1998  18,3%  19,4%    15%    22%  24,9%  18,8%  15,1%
 1997  18,8%  18,7%  16,2%  22,7%  28,2%  18,8%  15,8%
 1996  18,4%  17,8%  15,8%    22%    28%  20,3%  16,1%
 1995  19,2%  19,1%  16,6%  23,9%    25%  21,3%  16,2%
 1994  20,4%  20,6%    19%  21,7%  26,7%    22%  17,5%
 1993  19,7%    20%    17%  22,9%  25,4%  21,8%  17,5%
 1992  21,2%  20,8%  19,7%  24,8%  22,8%  20,6%  21,4%
 1991  21,1%  19,9%  22,8%  20,7%  23,4%  20,4%    19%
 1990  18,3%  18,1%  17,6%  20,3%  19,8%  20,1%  16,1%
 1989  17,2%  17,6%  16,1%    17%  20,9%  19,5%  13,3%
 1988  16,8%  16,9%  16,6%  17,2%  18,8%  19,6%    12%
 1987  17,7%  17,2%  17,2%  20,1%  19,2%  21,7%  12,1%
 1986  18,1%  19,7%  16,6%  16,6%  19,2%  25,2%  11,9%
 1985  17,7%    20%  16,5%  15,2%  17,5%  24,3%  10,3%
 1984  18,9%  21,3%    18%    17%  15,9%  22,2%   9,9%
0 голосов
/ 19 июня 2020

Попробуйте:

import urllib.request
import pandas as pd
from tabulate import tabulate

source = urllib.request.urlopen('https://de.statista.com/statistik/daten/studie/185581/umfrage/mittlere-kronenverlichtung-in-deutschland-seit-1984/').read()
soup = bs.BeautifulSoup(source,'lxml')
table = soup.find('table')
table_rows = table.find_all('tr')

columns = ["Jahr", "Gesamt/ alle Baumarten", "Fichte",
           "Kiefer", "Buche", "Eiche", "andere Nadelbäume", 
           "andere Laubbäume"]

items = {i:[] for i in columns}

for tr in table_rows:
    td = tr.find_all('td')
    row = [i.text for i in td]

    if row:
        items["Jahr"].append(row[0])
        items["Gesamt/ alle Baumarten"].append(row[1])
        items["Fichte"].append(row[2])
        items["Kiefer"].append(row[3])
        items["Buche"].append(row[4])
        items["Eiche"].append(row[5])
        items["andere Nadelbäume"].append(row[6])
        items["andere Laubbäume"].append(row[7])

df = pd.DataFrame(items)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...