程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

yield(0)

面向对象(0)

标签  

字典(0)

列表(0)

日期归档  

python 小练习

发布于2020-02-21 11:19     阅读(1122)     评论(0)     点赞(24)     收藏(4)


1、将txt文件写入到excel

import xlwt #写入文件
import os

# main function:将txt文件写入到excel

def txt_to_excel(sheetName,excelFileName,txtFileName,col_num):
'''
:param sheetName: sheet名称
:param excelFileName: Excel文件名称
:param txtFileName: txt文件名称
:param col_num: 列数
:return:
'''
excelFile = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = excelFile.add_sheet(sheetName)

if os.path.exists(excelFileName):
os.remove(excelFileName)
print("excel文件去重")
i=0
j=0
with open(txtFileName,'r',encoding='utf-8') as f:
lines = f.readlines()
print(lines)
for line in lines:
#print(line)
sheet.write(i,j,line)
j=j+1
if(j%col_num==0):
i=i+1
j=0
excelFile.save(excelFileName)

print("excel文件写入完成")



if __name__ == '__main__':
txtFileName = 'info.txt'
excelFileName = 'info.xls'
sheetName = "sheet1"
col_num = 3
txt_to_excel(sheetName,excelFileName,txtFileName,col_num)


2、输出一个字符串中出现次数最多的字符及其次数
def get_duplicate_num(string):
f=list(string)
g=list(string)
count=0
h={}
for i in range(len(g)):
for j in range(len(f)):
if f[j]==g[i]:
count=count+1
h[g[i]]=count
count=0
# print(h)
# print(max(h.values()))
for k,v in h.items():
if v==max(h.values()):
print("出现次数最多的字符为{},出现的最大次数是{}".format(k,v))

if __name__ == '__main__':
string = "jjdfkja2343"
get_duplicate_num(string)


3、校验输入的一个字符串是否是有效的身份证,如有效,输出该身份证号对应的地址及性别。
import xlrd
import os
import datetime

#读取excel内容判断所在省,市/县(excel第一列为市/县编号,第二列为市/县,第三列为省编号,第四列为省名
def excel_check(card):
num = card[0:6]
file_path = (os.path.join(os.path.abspath('.'), '身份证.xlsx'))
data = xlrd.open_workbook(file_path)
tab = data.sheet_by_name('idcode')
row_num = tab.nrows
# print(row_num)
dict = {}
for i in range(0, row_num):
key = tab.cell_value(i, 0)
value = tab.cell_value(i, 1)
dict[key] = value
#print(dict)

dict1 = {}
for j in range(0, 34):
key1 = tab.cell_value(j, 2)
value1 = tab.cell_value(j, 3)
dict1[key1] = value1
#print(dict1)

if num in dict.keys():
#print(dict1[num[0:2]]+dict[num])
return (dict1[num[0:2]]+dict[num])

else:
#print("身份证格式有误")
return False


#判断是否是日期
def validate(card):
date_text=card[6:14]
try:
datetime.datetime.strptime(str(date_text), '%Y%m%d')
#print(str(date_text)+"是一个正确的日期格式")
except ValueError:
raise ValueError("身份证格式有误")
return False

#判断男女
def sex_check(card):
data = int(card[14:17])
if data % 2 == 0:
return ("女性")
else:
return ("男性")

#校验身份证最后一位数
def last_data_check(card):
num = card[17:18]
ratio = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 2]
last_char = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]
card_data = list(card)
card_data = list(map(int, card_data[0:17]))
sum = 0
for i in range(17):
sum += ratio[i] * card_data[i]
# print("最后一位字符为{0}".format(last_char[sum % 11]))

if num != str(last_char[sum % 11]):
#print("身份证格式有误")
return False

if __name__ == '__main__':

IDcard='4654547879811222'

if len(IDcard)==18:
if excel_check(IDcard)==False or validate(IDcard)==False or last_data_check(IDcard)==False:
print("身份证格式有误")
else:
print(excel_check(IDcard))
print(sex_check(IDcard))
else:
print("身份证格式有误")


所属网站分类: 技术文章 > 博客

作者:goodbody

链接:https://www.pythonheidong.com/blog/article/231830/4072b1b05a6b774f21f9/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

24 0
收藏该文
已收藏

评论内容:(最多支持255个字符)