-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvertPrice.py
63 lines (51 loc) · 1.34 KB
/
convertPrice.py
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
import pymysql
USD_TO_RMB = 6.7765
USD_DICT = [
"USD",
"$",
"美元"
]
CLEAN_DICT = [
"USD",
"$",
"美元",
"CNY",
"¥",
"¥",
"人民币",
"元",
]
FIX_DICT = {
"。": ".",
}
def getRows() -> list[list[str, str]]:
cursor.execute("select isbn, price from stocks")
rows = cursor.fetchall()
return rows
def convertPrice(price_raw: str) -> float:
multiplier: float = 1
for word in USD_DICT:
if word in price_raw:
multiplier = USD_TO_RMB
break
price_text: str = price_raw
for word in CLEAN_DICT:
price_text = price_text.replace(word, "")
for key in FIX_DICT:
price_text = price_text.replace(key, FIX_DICT[key])
price: float = float(price_text)
# convert to RMB
price = price * multiplier
price = round(price, 2)
print("{} → {} → {}".format(price_raw, price_text, price))
return price
def price2cost() -> None:
rows = getRows()
for row in rows:
price = convertPrice(row[1])
cursor.execute("update stocks set price=%s, cost=%s where isbn=%s",
(price, int(price), row[0]))
conn = pymysql.connect(host="localhost", port=6033, user="crud",
passwd="ssr129631", db="bookstore", autocommit=True, charset='utf8')
cursor = conn.cursor()
price2cost()