python 스 크 립 트 는 txt 텍스트 를 sql 로 변환 하고 데 이 터 를 간단하게 검사 합 니 다.
14325 단어 python 진급
1. , 202004, 4 ,5 202005, ( )
2. ( utf-8, notebook , utf-8)
3. , 202004 202004_sql , sql
4. 202004_sql , mysql -uroot -p( )
5. sql
6.
#!/usr/bin/python
# translate txt_data to sql
#
import io
import os
# filename = input("filename:")
# print(filename)
base_dir = os.getcwd()
date_dir = input('date_dir:')
# %Y%m , _sql
sql_dir = base_dir + "/" + date_dir + "/" + date_dir + '_sql'
if os.path.exists(sql_dir):
pass
else:
os.mkdir(base_dir + "/" + date_dir + "/" + date_dir + '_sql')
def convert_data(filename):
sql_file = filename + ".sql"
with io.open(filename, 'r+', encoding='utf-8') as f:
for line in f:
# &,
#
if line:
line = line.replace(',', '&')
line = line.replace('`', ',')
L1 = line.split(',')
# , NULL
if len(L1) < 58:
x = 58 - len(L1)
for _ in range(x):
L1.append('NULL')
# NULL
for i in L1:
if i == '':
i = "NULL"
S1 = tuple(x for x in L1)
sql = "insert into " + "table_" + date_dir + "(`msg_type`, `sy_fn`, `trans_date`, `SP_code`, `trans_handle_code`, `ser_condition_code`, `trans_type_zh`, `acc_trans_date`, `acc_trans_time`, `trans_amount`, `term_no`, `term_serial_no`, `mer_code`, `term_no_cups`, `mer_code_cups`, `pay_code_type`, `pay_no`, `bill_no_type`, `bill_no`, `user_area_code`, `user_att_area_code`, `card_no`, `industry_org_code`, `bank_code`, `term_org_trans_flow`, `system_org_trans_flow`, `liq_date`, `Ret_Reference_no`, `Auth_code_38`, `54_domain`, `Receive_mechanism_id`, `trans_status`, `cups_response_code`, `Acc_response_code`, `Industry_respons_code`, `trans_channel_60`, `trans_code`, `acc_reservation_122`, `multi_channel_error_code`, `add_industry_info`, `retain_add_info`, `send_mechanism_code`, `std_bus_model`, `std_enter_org_name`, `std_bus_no`, `std_bus_name`, `std_bus_auth_no`, `std_user_no_types`, `std_user_no`, `std_user_area_no`, `std_user_no_area_add_code`, `rece_institution_id_code`, `convert_org_code`, `msg_resv_fld`, `mer_code2`, `mix`, `ind_mer_code`, `mer_pay_add`) values{};
".format(S1)
sql_file = os.path.join(sql_dir, sql_file)
with open(sql_file, 'a+') as e:
e.write(sql)
# convert the data to sql
def check_data():
"""
data sql
"""
check_dir = os.path.join(base_dir, date_dir)
files = os.listdir(check_dir)
os.chdir(check_dir)
SUM = 0
for filename in files:
if not os.path.isdir(filename):
with open(filename, 'r') as f:
n = len(f.readlines())
SUM += n
print("source_data_lines:", SUM)
sql_files = os.listdir(sql_dir)
os.chdir(sql_dir)
count = 0
for file in sql_files:
if not os.path.isdir(file):
with open(file, 'r') as e:
m = len(e.readlines())
count += m
print("sql_data_lines:", count)
if __name__ == '__main__':
source_data_dir = os.path.join(base_dir, date_dir)
files = os.listdir(source_data_dir)
os.chdir(source_data_dir)
for filename in files:
if not os.path.isdir(filename):
convert_data(filename)
# pass
check_data()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
풍화 프로 그래 밍 -- python 문자열 str 구명 모듈 namedtuple 목록 목록 유도 식 및 바이트 그룹 byterayarray 는 같은 기본 형식 (문자, 바이트, 수치) 의 데 이 터 를 저장 합 니 다 (연속 적 인 메모리 공간 을 차지 합 니 다) else 판단 을 앞 에 놓 습 니 다 [result 1 if expressi...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.