mdbq 0.1.4__py3-none-any.whl → 0.1.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mdbq/dataframe/converter.py +8 -2
- mdbq/mysql/s_query.py +32 -1
- {mdbq-0.1.4.dist-info → mdbq-0.1.6.dist-info}/METADATA +1 -1
- {mdbq-0.1.4.dist-info → mdbq-0.1.6.dist-info}/RECORD +6 -6
- {mdbq-0.1.4.dist-info → mdbq-0.1.6.dist-info}/WHEEL +0 -0
- {mdbq-0.1.4.dist-info → mdbq-0.1.6.dist-info}/top_level.txt +0 -0
mdbq/dataframe/converter.py
CHANGED
@@ -24,6 +24,7 @@ class DataFrameConverter(object):
|
|
24
24
|
df.replace(to_replace=['="'], value='', regex=True, inplace=True) # ="和"不可以放在一起清洗, 因为有: id=86785565
|
25
25
|
df.replace(to_replace=['"'], value='', regex=True, inplace=True)
|
26
26
|
cols = df.columns.tolist()
|
27
|
+
|
27
28
|
for col in cols:
|
28
29
|
# df[col] = df[col].apply(lambda x: re.sub('[="]', '', str(x)) if '="' in str(x) else x)
|
29
30
|
# 百分比在某些数据库中不兼容, 转换百分比为小数
|
@@ -31,9 +32,14 @@ class DataFrameConverter(object):
|
|
31
32
|
# 尝试转换合适的数据类型
|
32
33
|
if df[col].dtype == 'object':
|
33
34
|
try:
|
34
|
-
df[col] = df[col].astype(int) # 尝试转换 int
|
35
|
+
# df[col] = df[col].astype(int) # 尝试转换 int
|
36
|
+
df[col] = df[col].apply(lambda x: int(x) if '_' not in str(x) else x)
|
35
37
|
except:
|
36
|
-
df[col] = df[col].astype('float64', errors='ignore') # 尝试转换 float, 报错则忽略
|
38
|
+
# df[col] = df[col].astype('float64', errors='ignore') # 尝试转换 float, 报错则忽略
|
39
|
+
try:
|
40
|
+
df[col] = df[col].apply(lambda x: float(x) if '_' not in str(x) else x)
|
41
|
+
except:
|
42
|
+
pass
|
37
43
|
if df[col].dtype == 'float': # 对于小数类型, 保留 6 位小数
|
38
44
|
df[col] = df[col].apply(lambda x: round(float(x), 6) if x != 0 else x)
|
39
45
|
# 清理列名, 在 mysql 里面列名不能含有某些特殊字符
|
mdbq/mysql/s_query.py
CHANGED
@@ -86,6 +86,33 @@ class QueryDatas:
|
|
86
86
|
print(f'database: {db_name}, table: {tabel_name} 查询的数据为空')
|
87
87
|
return df
|
88
88
|
|
89
|
+
def columns_to_list(self, db_name, tabel_name, columns_name) -> list:
|
90
|
+
"""
|
91
|
+
获取数据表的指定列, 返回列表
|
92
|
+
[{'视频bv号': 'BV1Dm4y1S7BU', '下载进度': 1}, {'视频bv号': 'BV1ov411c7US', '下载进度': 1}]
|
93
|
+
"""
|
94
|
+
if self.check_infos(db_name, tabel_name) == False: # 检查传入的数据库和数据表是否存在
|
95
|
+
return []
|
96
|
+
|
97
|
+
self.config.update({'database': db_name})
|
98
|
+
connection = pymysql.connect(**self.config) # 重新连接数据库
|
99
|
+
with connection.cursor() as cursor:
|
100
|
+
# 3. 获取数据表的所有列信息
|
101
|
+
sql = 'SELECT COLUMN_NAME FROM information_schema.columns WHERE table_schema = %s AND table_name = %s'
|
102
|
+
cursor.execute(sql, (db_name, {tabel_name}))
|
103
|
+
columns = cursor.fetchall()
|
104
|
+
cols_exist = [col['COLUMN_NAME'] for col in columns] # 数据表的所有列, 返回 list
|
105
|
+
columns_name = [item for item in columns_name if item in cols_exist]
|
106
|
+
if len(columns_name) == 0:
|
107
|
+
return []
|
108
|
+
columns_in = ', '.join(columns_name)
|
109
|
+
sql = (f"SELECT {columns_in} FROM {db_name}.{tabel_name} ")
|
110
|
+
cursor.execute(sql)
|
111
|
+
column_values = cursor.fetchall() # 返回指定列,结果是[dict, dict, dict, ...]
|
112
|
+
# column_values = [item[column_name] for item in column_values] # 提取字典的值, 组成列表
|
113
|
+
connection.close()
|
114
|
+
return column_values
|
115
|
+
|
89
116
|
def check_infos(self, db_name, tabel_name) -> bool:
|
90
117
|
""" 检查数据库、数据表是否存在 """
|
91
118
|
connection = pymysql.connect(**self.config) # 连接数据库
|
@@ -119,5 +146,9 @@ class QueryDatas:
|
|
119
146
|
|
120
147
|
|
121
148
|
if __name__ == '__main__':
|
122
|
-
username, password, host, port = get_myconf.select_config_values(target_service='
|
149
|
+
username, password, host, port = get_myconf.select_config_values(target_service='home_lx', database='mysql')
|
123
150
|
print(username, password, host, port)
|
151
|
+
|
152
|
+
q = QueryDatas(username, password, host, port)
|
153
|
+
res = q.columns_to_list(db_name='视频数据', tabel_name='bilibili视频', columns_name=['视频bv号', '下载进度'])
|
154
|
+
print(res)
|
@@ -13,14 +13,14 @@ mdbq/config/__init__.py,sha256=jso1oHcy6cJEfa7udS_9uO5X6kZLoPBF8l3wCYmr5dM,18
|
|
13
13
|
mdbq/config/get_myconf.py,sha256=9v3xebfcS1tptxpvk3_tGxfXjAehGVCveYe4iRUzLQQ,6372
|
14
14
|
mdbq/config/update_conf.py,sha256=YjGjjRchu5BcrmLJkoLjHEF2TbGOmsgCWX4LroXOYWQ,3455
|
15
15
|
mdbq/dataframe/__init__.py,sha256=2HtCN8AdRj53teXDqzysC1h8aPL-mMFy561ESmhehGQ,22
|
16
|
-
mdbq/dataframe/converter.py,sha256=
|
16
|
+
mdbq/dataframe/converter.py,sha256=h_BDc6oNmMCVFOUzZJq4nXNGDyJFJyycpWqlyrv7U04,3089
|
17
17
|
mdbq/log/__init__.py,sha256=Mpbrav0s0ifLL7lVDAuePEi1hJKiSHhxcv1byBKDl5E,15
|
18
18
|
mdbq/log/mylogger.py,sha256=oaT7Bp-Hb9jZt52seP3ISUuxVcI19s4UiqTeouScBO0,3258
|
19
19
|
mdbq/mongo/__init__.py,sha256=SILt7xMtQIQl_m-ik9WLtJSXIVf424iYgCfE_tnQFbw,13
|
20
20
|
mdbq/mongo/mongo.py,sha256=q0B4wXDSTtXg_vMN7MPh6zdxl6tT68tM74LmdVNQQek,31892
|
21
21
|
mdbq/mysql/__init__.py,sha256=A_DPJyAoEvTSFojiI2e94zP0FKtCkkwKP1kYUCSyQzo,11
|
22
22
|
mdbq/mysql/mysql.py,sha256=w_FDE4ulOnHdfIjjXB1Ff_cnXyx2PuBAQWGjTYyVm3U,30629
|
23
|
-
mdbq/mysql/s_query.py,sha256=
|
23
|
+
mdbq/mysql/s_query.py,sha256=mNrdyMeiQ5wQVD_9IDTrk6jrMtszQftYQg3AwNWhROc,6997
|
24
24
|
mdbq/mysql/year_month_day.py,sha256=VgewoE2pJxK7ErjfviL_SMTN77ki8GVbTUcao3vFUCE,1523
|
25
25
|
mdbq/other/__init__.py,sha256=jso1oHcy6cJEfa7udS_9uO5X6kZLoPBF8l3wCYmr5dM,18
|
26
26
|
mdbq/other/porxy.py,sha256=UHfgEyXugogvXgsG68a7QouUCKaohTKKkI4RN-kYSdQ,4961
|
@@ -30,7 +30,7 @@ mdbq/pbix/__init__.py,sha256=Trtfaynu9RjoTyLLYBN2xdRxTvm_zhCniUkVTAYwcjo,24
|
|
30
30
|
mdbq/pbix/pbix_refresh.py,sha256=JUjKW3bNEyoMVfVfo77UhguvS5AWkixvVhDbw4_MHco,2396
|
31
31
|
mdbq/pbix/refresh_all.py,sha256=wulHs4rivf4Mi0Pii2QR5Nk9-TBcvSwnCB_WH9QULKE,5939
|
32
32
|
mdbq/spider/__init__.py,sha256=RBMFXGy_jd1HXZhngB2T2XTvJqki8P_Fr-pBcwijnew,18
|
33
|
-
mdbq-0.1.
|
34
|
-
mdbq-0.1.
|
35
|
-
mdbq-0.1.
|
36
|
-
mdbq-0.1.
|
33
|
+
mdbq-0.1.6.dist-info/METADATA,sha256=a5jG4lDS-9TA8xU5IInf--XK_X0P84L9FOZQY-ysGHU,245
|
34
|
+
mdbq-0.1.6.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
35
|
+
mdbq-0.1.6.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
|
36
|
+
mdbq-0.1.6.dist-info/RECORD,,
|
File without changes
|
File without changes
|