Skip to content

Commit 474833b

Browse files
committed
issue correct
1 parent 78201d7 commit 474833b

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

basic/mycore/logmsg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
__author__ = 'Xiong Neng'
1818
logging.basicConfig(level=logging.INFO,
1919
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
20-
datefmt='%Y-%m-%d %H:%M:%S')
20+
datefmt='%Y-%m-%d %H:%M:%S',
21+
handlers=[logging.FileHandler('message.log', 'a', 'utf-8')])
2122
# 模块基本用_,类级别用__
2223
_log = logging.getLogger('app.' + __name__)
2324

basic/myutils/excel/excel_mysql.py

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
sql_create1 = """
1818
CREATE TABLE t_enterprise (
1919
id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
20-
name VARCHAR(300) NOT NULL COMMENT '企业名称',
21-
tax_code VARCHAR(60) NOT NULL COMMENT '税号',
20+
name VARCHAR(100) COMMENT '企业名称',
21+
tax_code VARCHAR(30) COMMENT '税号',
2222
region_id BIGINT COMMENT '区域ID',
2323
customer_type INTEGER COMMENT '客户类型',
2424
enterprise_type INTEGER COMMENT '企业类型',
2525
address VARCHAR(200) COMMENT '详细地址',
2626
postcode VARCHAR(10) COMMENT '邮编',
27-
tel VARCHAR(30) COMMENT '联系电话',
27+
tel VARCHAR(50) COMMENT '联系电话',
2828
contact VARCHAR(10) COMMENT '联系人',
2929
fax VARCHAR(30) COMMENT '传真',
30-
mobile VARCHAR(30) COMMENT '手机号',
30+
mobile VARCHAR(16) COMMENT '手机号',
3131
created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
3232
updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
3333
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT '企业表';
@@ -37,7 +37,7 @@
3737
id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
3838
region_code VARCHAR(16) COMMENT '邮编',
3939
regian_name VARCHAR(20) COMMENT '区域名',
40-
note VARCHAR(120) COMMENT '备注',
40+
note VARCHAR(200) COMMENT '备注',
4141
parent_id BIGINT COMMENT '父级ID',
4242
created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
4343
updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
@@ -66,14 +66,11 @@
6666
"""
6767
logging.basicConfig(level=logging.INFO,
6868
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
69-
datefmt='%Y-%m-%d %H:%M:%S')
69+
datefmt='%Y-%m-%d %H:%M:%S',
70+
handlers=[logging.FileHandler('excel.log', 'a', 'utf-8')])
7071
_log = logging.getLogger('app.' + __name__)
7172

7273

73-
def _log_info(msgpre):
74-
_log.info('{}---{}'.format(msgpre, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
75-
76-
7774
def _connect():
7875
config = {
7976
'user': 'root',
@@ -117,13 +114,16 @@ def parse_sheet(wb, sheet_name, column_num, log_msg):
117114
break
118115
row_data.append(cell.value)
119116
result_list.append(row_data[:])
120-
_log_info(log_msg)
117+
_log.info(log_msg)
121118
return result_list
122119

123120

121+
def handle_wrong_line(wrong_line):
122+
pass
123+
124124
def xlsx_to_table(xlsx_name):
125125
conn_ = _connect()
126-
_log_info('Excel文件解析start')
126+
_log.info('Excel文件解析start')
127127
wb = load_workbook(xlsx_name, read_only=True)
128128
# 先收集企业资料表
129129
list1 = parse_sheet(wb, 'customer', 6, 'customer表解析end')
@@ -134,36 +134,44 @@ def xlsx_to_table(xlsx_name):
134134
# 收集区域表
135135
list3 = parse_sheet(wb, 'region', 5, 'region表解析end')
136136
data3 = [(v[0], v[1], v[2], v[3], v[4]) for v in list3[1:]]
137-
_log_info('Excel文件解析end')
138-
137+
_log.info('Excel文件解析end')
138+
_log.info('---------------------------分割线--------------------------------')
139+
_log.info('数据库更新start')
139140
cursor = conn_.cursor()
140-
141-
_log_info('插入企业资料start')
142-
for i, d1 in enumerate(data1):
143-
cursor.execute(sql_insert_enterprise, d1)
144-
if i % 50 == 0:
145-
conn_.commit()
146-
conn_.commit()
147-
_log_info('插入企业资料end')
148-
149-
_log_info('更新企业联系信息start')
150-
for i, d2 in enumerate(data2):
151-
cursor.execute(sql_update_enterprise, d2)
152-
if i % 50 == 0:
153-
conn_.commit()
154-
conn_.commit()
155-
_log_info('插入企业资料表end')
156-
157-
_log_info('插入区域信息start')
158-
for i, d3 in enumerate(data3):
159-
cursor.execute(sql_insert_region, d3)
160-
if i % 50 == 0:
161-
conn_.commit()
162-
conn_.commit()
163-
_log_info('插入区域信息end')
164-
165-
_log_info('数据库更新end')
166-
141+
try:
142+
_log.info('插入企业资料start')
143+
for i, d1 in enumerate(data1):
144+
if len(d1[1]) > 300 and not d1[2]:
145+
_log.error('这一行有问题')
146+
handle_wrong_line(d1)
147+
continue
148+
cursor.execute(sql_insert_enterprise, d1)
149+
if i % 50 == 0:
150+
conn_.commit()
151+
conn_.commit()
152+
_log.info('插入企业资料end')
153+
154+
_log.info('更新企业联系信息start')
155+
for i, d2 in enumerate(data2):
156+
cursor.execute(sql_update_enterprise, d2)
157+
if i % 50 == 0:
158+
conn_.commit()
159+
conn_.commit()
160+
_log.info('插入企业资料表end')
161+
162+
_log.info('插入区域信息start')
163+
for i, d3 in enumerate(data3):
164+
cursor.execute(sql_insert_region, d3)
165+
if i % 50 == 0:
166+
conn_.commit()
167+
conn_.commit()
168+
_log.info('插入区域信息end')
169+
170+
except:
171+
logging.exception('Got exception on db handler')
172+
raise
173+
174+
_log.info('数据库更新end')
167175
cursor.close()
168176
conn_.close()
169177

source/c01/p03_keep_last_n_items.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
for li in lines:
2626
if pattern in li:
2727
yield li, previous_lines
28-
previous_lines.append(line)
28+
previous_lines.append(li)
2929
3030
# Example use on a file
3131
if __name__ == '__main__':
3232
with open(r'../../cookbook/somefile.txt') as f:
33-
for line, prevlines in search(f, 'Python', 5):
33+
for line, prevlines in search(f, 'python', 5):
3434
for pline in prevlines:
3535
print(pline, end='')
3636
print(line, end='')

0 commit comments

Comments
 (0)