To save the Python program, you can use the following method: use the editor to write the code and save
1、 Open Notepad++
2、 When you need to create a new text, click "Text" in the upper left corner, and click "New" in the pop-up menu bar.
3、 At the same time, you can also directly click the new icon under "Text".
4、 When saving, save it as a .py file, here it is saved as hello.py
Content expansion:
Temporary storage of python programs
#! /usr/bin/env python
# - *- coding:utf-8-*-
# Import regular module
import re
# Import the os module, this example uses it for file writing operations
import os
# Import log module
import logging
import logging.handlers
import xlwt
import xlrd
import MySQLdb
import pymysql
db = pymysql.connect("localhost","root","123456","my_db_hanchuan01")
# Iterate over files+Method of collecting mobile phone number
def count(dirname='D:/tmp/Temporary data source/sheet conversion/txtfiles'):
#- - - - - - - - - - - - - - - - - - - - - - - Regular-------------------------------- #手机号Regular
pattern_mob = re.compile('1[345789]\d{9}')
# pattern_mob_plus = re.sub('1[345789]\d{9}','1[345789]\d{9}+123')
#- - - - - - - - - - - - - - - - - - - - - Statistics file------------------------------ #Variable to record the total number of files
count=0
num=0 #Variables that record the number of files containing mobile phone numbers
for root, dirs, files in os.walk(dirname):
# Get the number of files in each folder
for filename in files:
# Concatenate the file name and path into a full path
f =open(dirname+'/'+filename,'r+', encoding='utf-8')
var1 =(filename) #Write file name to database
result = pattern_mob.findall(f.read()) # result = pattern_mob_plus.findall(f.read()) # result = pattern_mob.findall # result = result+"123"for phonenames in result:
var2 =(phonenames)print(var1)print(var2)
cursor = db.cursor() #Use cursor()Method to create a cursor object cursor
cursor.execute("INSERT INTO 00_HEFEI_PHONES VALUES('%s', %s)"%(var1,var2)) #Execute sql statement
db.commit()
# Main function
if __name__ =='__main__':
# # Call count()h function
count()
db.close()
So far, this article on how to save python programs is introduced. For more related python program saving methods, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. I hope you will support ZaLou.Cn more in the future. !
Recommended Posts