Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
重
重点类信息提取
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ZGC_INDEX
重点类信息提取
Commits
4df69b5b
Commit
4df69b5b
authored
Apr 27, 2021
by
rico.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
类别更新
parent
ec07337d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
229 additions
and
0 deletions
+229
-0
类别更新导入数据库.py
公共代码/类别更新导入数据库.py
+229
-0
No files found.
公共代码/类别更新导入数据库.py
0 → 100644
View file @
4df69b5b
import
uuid
import
pandas
as
pd
import
pymssql
# coding=utf-8
#读取excel中的内容
df
=
pd
.
read_excel
(
'处理后数据-扫描仪数据0407.xlsx'
,
sheet_name
=
2
,
converters
=
{
'产品名称'
:
str
,
'类别名称'
:
str
,
'产品品牌'
:
str
,
'类别编码'
:
str
,
'产品编码'
:
str
}
)
#SKU命名
def
all_modify_SKU_name
(
product_name
,
category_name
):
list_temp
=
[]
#需要输出的列表
index_row
=
0
#从excel中获取两个列的数据,类型为list
product_name_list
=
df
[
product_name
]
.
tolist
()
category_name_list
=
df
[
category_name
]
.
tolist
()
#替换产品名称中错误的类别
for
i
in
product_name_list
:
temp
=
i
.
replace
(
"扫描仪"
,
category_name_list
[
index_row
])
if
(
temp
==
i
):
temp_1
=
i
.
replace
(
"扫描枪"
,
category_name_list
[
index_row
])
list_temp
.
append
(
temp_1
)
else
:
list_temp
.
append
(
temp
)
#判断长度
if
(
index_row
<
len
(
product_name_list
)
-
1
):
index_row
+=
1
#遍历完列表
else
:
print
(
"Through the list"
)
#检查替换后的列表
SKU_name
=
list_temp
return
SKU_name
#SPU命名
def
all_modify_SPU_name
(
product_brand
,
category_name
):
list_temp
=
[]
index_row
=
0
#从excel中获取两个列的数据,类型为list
product_brand_list
=
df
[
product_brand
]
.
tolist
()
category_name_list
=
df
[
category_name
]
.
tolist
()
#合并两项成为SPU
for
i
in
product_brand_list
:
temp
=
product_brand_list
[
index_row
]
+
' '
+
category_name_list
[
index_row
]
list_temp
.
append
(
temp
)
index_row
+=
1
SPU_name
=
list_temp
return
SPU_name
def
all_match_SPU_name
(
DATABASE
,
TEST_DATABASE
,
SPU_name
,
CATEGORY_id
,
BRAND_id
):
# host 数据库服务器名称或IP
# user 用户名
# password 密码
# database 数据库名称
#连接数据库
conn
=
pymssql
.
connect
(
host
=
'123.56.115.207'
,
user
=
'zgcprice3311'
,
password
=
'zgcprice20200628'
,
database
=
TEST_DATABASE
,
autocommit
=
True
)
#创建游标
cursor_database
=
conn
.
cursor
()
conn_test
=
pymssql
.
connect
(
host
=
'123.56.115.207'
,
user
=
'zgcprice3311'
,
password
=
'zgcprice20200628'
,
database
=
TEST_DATABASE
,
autocommit
=
True
)
#创建游标
cursor_test
=
conn_test
.
cursor
()
#需要输出的列表
list_spu_update
=
[]
#计算找到和没找到的SPU的名字
match_number
=
0
unmatch_number
=
0
index_row
=
0
for
i
in
SPU_name
:
sql
=
(
"SELECT spu FROM p_spu WHERE spuname =
%
s"
)
cursor_database
.
execute
(
sql
,
i
)
spu_update_tuple
=
cursor_database
.
fetchone
()
if
(
spu_update_tuple
is
None
):
spu
=
str
(
uuid
.
uuid1
())
.
replace
(
'-'
,
''
)
#spu编码
list_spu_update
.
append
(
spu
)
print
(
"SPU created by uuid"
)
sql_insert
=
"INSERT INTO p_spu(spuname, spu, categoryid, brandid) VALUES (
%
s,
%
s,
%
s,
%
s)"
print
(
i
)
print
(
spu
)
print
(
CATEGORY_id
[
index_row
])
print
(
BRAND_id
[
index_row
])
列名
1
=
i
列名
2
=
spu
列名
3
=
CATEGORY_id
[
index_row
]
列名
4
=
BRAND_id
[
index_row
]
values
=
(
列名
1
,
列名
2
,
列名
3
,
列名
4
)
cursor_test
.
execute
(
sql_insert
,
values
)
index_row
+=
1
unmatch_number
+=
1
else
:
spu_update
=
spu_update_tuple
[
0
]
list_spu_update
.
append
(
spu_update
)
print
(
"SPU founded in database"
)
match_number
+=
1
index_row
+=
1
#print(list_spu_update)
print
(
"The number of matched data is"
)
print
(
match_number
)
print
(
"The number of unique data is"
)
print
(
unmatch_number
)
conn
.
close
()
conn_test
.
close
()
return
list_spu_update
def
all_get_category_id
(
category_id
):
category_id_list
=
df
[
category_id
]
.
tolist
()
#print(category_id_list)
return
category_id_list
def
all_get_brand_id
(
product_brand
,
DATABASE
):
product_brand_list
=
df
[
product_brand
]
.
tolist
()
list_brand_id_update
=
[]
conn
=
pymssql
.
connect
(
host
=
'123.56.115.207'
,
user
=
'zgcprice3311'
,
password
=
'zgcprice20200628'
,
database
=
DATABASE
,
autocommit
=
True
)
#创建游标
cursor_read
=
conn
.
cursor
()
for
i
in
product_brand_list
:
#print(i)
sql
=
(
"SELECT id FROM p_brand WHERE name =
%
s"
)
cursor_read
.
execute
(
sql
,
i
)
brand_update_id_tuple
=
cursor_read
.
fetchone
()
#判断产品ID是否存在
if
(
brand_update_id_tuple
[
0
]
==
""
):
print
(
"此产品项不存在"
)
list_brand_id_update
.
append
(
"产品id需补充"
)
else
:
list_brand_id_update
.
append
(
brand_update_id_tuple
[
0
])
conn
.
close
()
return
(
list_brand_id_update
)
def
delete_data
(
SPU_name
,
DATABASE
):
conn
=
pymssql
.
connect
(
host
=
'123.56.115.207'
,
user
=
'zgcprice3311'
,
password
=
'zgcprice20200628'
,
database
=
DATABASE
,
autocommit
=
True
)
cursor
=
conn
.
cursor
()
for
i
in
SPU_name
:
print
(
i
)
sql
=
"DELETE FROM p_spu WHERE spuname =
%
s"
cursor
.
execute
(
sql
,
i
)
conn
.
close
()
def
get_sku
(
sku
):
sku_list
=
df
[
sku
]
.
tolist
()
print
(
sku_list
)
return
(
sku_list
)
def
get_spu_id
(
spu
,
DATABASE
):
spu_id_list
=
[]
conn
=
pymssql
.
connect
(
host
=
'123.56.115.207'
,
user
=
'zgcprice3311'
,
password
=
'zgcprice20200628'
,
database
=
DATABASE
,
autocommit
=
True
)
#创建游标
cursor_read
=
conn
.
cursor
()
for
i
in
spu
:
print
(
i
)
#print(i)
sql
=
(
"SELECT id FROM p_spu WHERE spu =
%
s"
)
cursor_read
.
execute
(
sql
,
i
)
temp
=
cursor_read
.
fetchone
()
spu_id_list
.
append
(
temp
[
0
])
#判断产品ID是否存在
print
(
spu_id_list
)
conn
.
close
()
return
(
spu_id_list
)
def
write_data_to_excel
(
sku
,
spu
,
spu_id
):
sku_excel
=
sku
spu_excel
=
spu
spu_id_excel
=
spu_id
df
=
pd
.
DataFrame
.
from_dict
({
'sku'
:
sku_excel
,
'spu'
:
spu_excel
,
'spu_id'
:
spu_id_excel
})
df
.
to_excel
(
'data.xlsx'
,
header
=
True
,
index
=
False
)
def
update_sku_data
(
DATABASE
,
SKU
,
SKU_name
,
SPU_ID
):
#连接数据库
conn
=
pymssql
.
connect
(
host
=
'123.56.115.207'
,
user
=
'zgcprice3311'
,
password
=
'zgcprice20200628'
,
database
=
DATABASE
,
autocommit
=
True
)
#创建游标
cursor_read
=
conn
.
cursor
()
j
=
0
for
i
in
SKU_name
:
sql
=
"SELECT id FROM p_sku where skuname =
%
s"
sql_1
=
"UPDATE p_sku SET skuname =
%
s WHERE sku =
%
s"
#1.更新p_sku中的skuname,通过表中的sku进行匹配
sql_2
=
"UPDATE p_sku SET spuid =
%
s WHERE sku =
%
s"
#2.更新p_sku中的spuid,通过表中的sku进行匹配
cursor_read
.
execute
(
sql
,
i
)
result
=
cursor_read
.
fetchone
()
if
(
result
is
None
):
values_1
=
(
SKU_name
[
j
],
SKU
[
j
])
values_2
=
(
SPU_ID
[
j
],
SKU
[
j
])
cursor_read
.
execute
(
sql_1
,
values_1
)
cursor_read
.
execute
(
sql_2
,
values_2
)
else
:
values_2
=
(
SPU_ID
[
j
],
SKU
[
j
])
cursor_read
.
execute
(
sql_2
,
values_2
)
j
+=
1
print
(
"The work is done for category update"
)
sku
=
'产品编码'
product_brand
=
'产品品牌'
product_name
=
'产品名称'
category_name
=
'类别名称'
category_id
=
'类别编码'
database
=
'ZI_NEW'
test_database
=
'ZI_NEW_TEST'
SKU_name
=
all_modify_SKU_name
(
product_name
,
category_name
)
SPU_name
=
all_modify_SPU_name
(
product_brand
,
category_name
)
CATEGORY_id
=
all_get_category_id
(
category_id
)
BRAND_id
=
all_get_brand_id
(
product_brand
,
database
)
SPU
=
all_match_SPU_name
(
database
,
database
,
SPU_name
,
CATEGORY_id
,
BRAND_id
)
SKU
=
get_sku
(
sku
)
SPU_ID
=
get_spu_id
(
SPU
,
database
)
update_sku_data
(
database
,
SKU
,
SKU_name
,
SPU_ID
)
#delete_data(SPU_name, test_database)删库内数据用的,少用
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment