Python数据库字段拆分数据
生活随笔
收集整理的這篇文章主要介紹了
Python数据库字段拆分数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
7 Python案例
7.2 拆分?jǐn)?shù)據(jù)
7.2.1 解析數(shù)據(jù)庫(kù)字段里的數(shù)據(jù)
需求:從數(shù)據(jù)庫(kù)字段里解析得到結(jié)構(gòu)化數(shù)據(jù)。
解決方法:通過Python的pandas以及內(nèi)置的函數(shù)完成該需求。
?
?Python代碼:
?
import numpy as np import pandas as pd import os from sqlalchemy import create_engine import pymysql conn = pymysql.connect(host='192.168.56.32',port = 3306,user='test',passwd='qwert@765',db ='mydb',charset='utf8' )def tidy_split(df, column, sep='|', keep=False):"""Split the values of a column and expand so the new DataFrame has one splitvalue per row. Filters rows where the column is missing.Params------df : pandas.DataFramedataframe with the column to split and expandcolumn : strthe column to split and expandsep : strthe string used to split the column's valueskeep : boolwhether to retain the presplit value as it's own rowReturns-------pandas.DataFrameReturns a dataframe with the same columns as `df`."""indexes = list()new_values = list()df = df.dropna(subset=[column])for i, presplit in enumerate(df[column].astype(str)):values = presplit.split(sep)if keep and len(values) > 1:indexes.append(i)new_values.append(presplit)for value in values:indexes.append(i)new_values.append(value)new_df = df.iloc[indexes, :].copy()new_df[column] = new_valuesreturn new_dfdef read_table(cur, sql_order):try:cur.execute(sql_order)??data = cur.fetchall()frame = pd.DataFrame(list(data))except:? # , e:frame = pd.DataFrame()# print e# continuereturn framedef splitRFIDLog(cur, sql_order):rfidLogtab=read_table(cur, sql_order)rfidLogtab.columns = ['skuid', 'rfid','deviceid','records']### 設(shè)置列的最大長(zhǎng)度pd.set_option('max_colwidth', 1000)newtab1=tidy_split(rfidLogtab.iloc[1:4,0:4], 'records', sep='<br >') ###僅取3行newtab1.columns=['skuid', 'rfid','deviceid','records']newtab1=newtab1[newtab1.records.str.contains("入庫(kù)") == False]newtab1['records'] = newtab1.records.str.replace(' 已售 ', '')newtab1['records'] = newtab1.records.str.replace(' 入柜 ', '')newtab2=tidy_split(newtab1,'records', sep='[')newtab3 = tidy_split(newtab2.iloc[1::2, :], 'records', sep='-')newtab4 = tidy_split(newtab3, 'records', sep=']')newtab4 = pd.DataFrame(newtab4.records.str.replace('deviceid=', ''))newtab5=newtab4.records.str.slice(0,21)newtab5=pd.DataFrame(newtab5,columns=['records'])print(newtab5)df = pd.DataFrame(newtab5.records.values.reshape(-1, 3),columns=['prestatus', 'endstatus','deviceid'])df = df.reset_index(drop=True)print(df)timedf=newtab2[newtab2.iloc[0::1, :].records.str.contains("deviceid") == False]#.iloc[0::1, :]timedf = timedf.reset_index(drop=True)splitRFIDLogTab=pd.concat([timedf,df],axis=1, join='inner')#將拆分的RFID的數(shù)據(jù)分到CSV中splitRFIDLogTab.to_csv('E:\\a.csv', sep=',', header=True, index=False)conn.commit()conn.close() if __name__ == '__main__':cur = conn.cursor()sql_order = "SELECT skuid, rfid, deviceid, records FROM main_log limit 20;"splitRFIDLog(cur,sql_order)將生成的結(jié)果寫入到csv中,見如下示例結(jié)果:
skuid | rfid | deviceid | records | prestatus | endstatus | deviceid |
1 | 5133C90F30DCC1EE00005133C90F30DCC1EE0000 | 898602c9981730091839 | 2017/11/2 10:52 | 3 | 4 | 898602c9981730091839 |
總結(jié)
以上是生活随笔為你收集整理的Python数据库字段拆分数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 老主机u盘启动不了怎么回事啊 老电脑U盘
- 下一篇: 突然u盘读取不出来怎么办 怎么处理突然读