Hướng dẫn convert html table to dictionary python - chuyển đổi bảng html thành từ điển python

Dưới đây là một giải pháp đầu tiên đọc vào GANDAS DATAFRAME, và sau đó chuyển đổi thành từ điển như trong đầu ra mong muốn của bạn:

import pandas as pd

dfs = pd.read_html(html_string)
df = dfs[0]  # pd.read_html reads in all tables and returns a list of DataFrames

Giving:

     0                                      1         2
0  Mon  2:00 pm - 3:00 pm  5:00 pm - 10:00 pm       NaN
1  Tue  2:00 pm - 3:00 pm  5:00 pm - 10:00 pm       NaN
2  Wed  2:00 pm - 3:00 pm  5:00 pm - 10:00 pm  Open now
3  Thu  2:00 pm - 3:00 pm  5:00 pm - 10:00 pm       NaN
4  Fri  2:00 pm - 3:00 pm  5:00 pm - 10:00 pm       NaN
5  Sat                     5:00 pm - 10:00 pm       NaN
6  Sun                                 Closed       NaN

Sau đó sử dụng groupby và khả năng hiểu từ điển:

summary = {k: v.iloc[0, 1].split('  ') for k, v in df.groupby(0)}

Giving:

{'Fri': ['2:00 pm - 3:00 pm', '5:00 pm - 10:00 pm'],
 'Mon': ['2:00 pm - 3:00 pm', '5:00 pm - 10:00 pm'],
 'Sat': ['5:00 pm - 10:00 pm'],
 'Sun': ['Closed'],
 'Thu': ['2:00 pm - 3:00 pm', '5:00 pm - 10:00 pm'],
 'Tue': ['2:00 pm - 3:00 pm', '5:00 pm - 10:00 pm'],
 'Wed': ['2:00 pm - 3:00 pm', '5:00 pm - 10:00 pm']}

Bạn có thể cần phải chỉnh sửa một chút nếu tách chính xác hai không gian sẽ không luôn hoạt động cho định dạng dữ liệu thời gian mở của bạn.

"

fordinmemo[k].split(","):dinmemo[k].split(","):

data+=""+d+""+="

"

data+=""+="

"

data=""+data+""="

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

Nhập hệ điều hànhos

nhập khẩu dưa chuapickle

Nhập khẩu Tkinter ASTKtkinter astk

Nhập tkinter.ttk asttktkinter.ttk asttk

def save_template ():save_template():

GlobalMemomemo

memo={={

"Da Pagare": "Tim, Enel",:"tim, enel",

"Wip":""":"""

Skate Jack - Trò chơi,

Matchpair - trò chơi,

Posizionamento - Bài báo cho Aziendaitalia,

"" """

}

Với Open ("Memo.pkl", "WB") Asfile:open("memo.pkl","wb")asfile:

pickle.dump(memo,file).dump(memo,file)

def lưu (sự kiện):save(event):

key=lst.get(lst.curselection())=lst.get(lst.curselection())

memo[key]=tbx.get("0.0",tk.END)[key]=tbx.get("0.0",tk.END)

In ("Thay đổi đã lưu")("Changes saved")

Với Open ("Memo.pkl", "WB") Asfile:open("memo.pkl","wb")asfile:

pickle.dump(memo,file).dump(memo,file)

def lưu (sự kiện):load_template():

In ("Thay đổi đã lưu")open("memo.pkl","rb")asfile:

memo=pickle.load(file)=pickle.load(file)

def load_template ():memo

Với Open ("Memo.pkl", "RB") Asfile:save_once():

save_template()()

returnMemoto_html(event):

data=""=""

forkinmemo:kin memo:

data+=""+k+""+="

"+k+""+d+"
"+data+"
"

print(data)(data)

def save_once ():open("file.html","w")asfile:

file.write(data).write(data)

os.startfile("file.html").startfile("file.html")

def to_html (sự kiện):

memo=load_template()=load_template()

Với Open ("File.html", "W") Asfile:

# Nhận dữ liệu từ Pickleshow(event):

# cửa sổ' call this to show the note in the tbx '''

mm=lst.get(lst.curselection())=lst.get(lst.curselection())

tbx.delete("0.0",tk.END).delete("0.0",tk.END)

tbx.insert(tk.END,memo[mm]).insert(tk.END,memo[mm])

show def (sự kiện):add_memo(event):

'' 'Gọi cái này để hiển thị ghi chú trong TBX' ''("Here add a key, asking the name of the key with simpledialog")

def add_memo (sự kiện):

In ("Ở đây thêm một phím, hỏi tên của phím với SimpleDialog")help():

đi qua' call this to show some help '''

print('''('''

DEF HELP ():

'' 'Gọi cái này để hiển thị một số trợ giúp' '

Bản ghi nhớ là từ điển

Key = Tên của sự kiện

value = multiline với những thứ cần nhớ được phân tách bởi dấu phẩy

LST là hộp danh sách với các phím

TBX là hộp văn bản'')

root=tk.Tk()=tk.Tk()

Nếu bạn chọn một khóa, bạn sẽ thấy các giá trị trong TBX.title("Memo - use , to separate the items for each memo")

'' ')

style=ttk.Style(root)=ttk.Style(root)

root.title ("Memo - sử dụng, để tách các mục cho mỗi bản ghi nhớ").tk.call('source','azure dark\\azure dark.tcl')

style.theme_use('azure').theme_use('azure')

frm1=tk.Frame(root) =tk.Frame(root)

frm1.pack(side="left",fill="both",expand=True).pack(side="left",fill="both",expand=True)

lst=tk.Listbox(frm1)= tk.Listbox(frm1)

lst.pack(side="left",fill="both",expand=True).pack(side="left",fill="both",expand=True)

forkinmemo:kinmemo:

lst.insert(tk.END,k).insert(tk.END,k)

frm2=tk.Frame(root)=tk.Frame(root)

frm2.pack(side="left").pack(side="left")

tbx=tk.Text(frm2)= tk.Text(frm2)

tbx.pack(side="left").pack(side="left")

lst.bind("",show).bind("<>",show)

root.bind("",save).bind("", save)

root.bind("",to_html).bind("",to_html)

root.bind("",add_memo).bind("<+>",add_memo)

root.mainloop().mainloop()