Swust Python 复习

发布于 2021-06-07  2 次阅读


一、文件I/O

1.1 读取CSV与遍历

  1. 导入csv库
  2. open文件
  3. 创建“表头”的tuple
  4. 使用csv.DictReader()函数
  5. 遍历使用for Name in,Name类型为OrderedDict,使用tuple元素作为下标访问
import csv

csvFile = open("test.csv", 'r', encoding = 'utf-8', errors = 'ignore')
# IO
fieldNames = ("城市", "环比", "同比", "定基")
# Tuple
csvRead = csv.DictReader(csvFile, fieldNames)
# DictReader,fieldNames省缺则以第一行为表头

for row in csvRead:
    print(row['城市'])
# End for

    值得注意的是,上述的遍历是从第0行开始的,即包含了文件本身的表头。若要消除文件自带表头,只需要在构造DictReader时省缺第二个参数。

1.2 csv转换json

  1. 导入csv与json库
  2. open文件
  3. 遍历csv,同时使用json.dump写入文件,同时使用write写入一个换行符
import csv
import json

csvFile = open("test.csv", 'r', encoding = 'utf-8', errors = 'ignore')
jsonFile = open("out.json", 'w', encoding = 'utf-8', errors = 'ignore')

fieldNames = ("城市", "环比", "同比", "定基")
csvRead = csv.DictReader(csvFile, fieldNames)
# fieldNames根据情况省略

for row in csvRead:
    json.dump(row, jsonFile)
    jsonFile.write('\n')
# End for

    同样,上述代码中可能包含表头,解析时按行解析。

1.3 open的参数选择

def OpenFile(p_mode):
    TxtFile = open("test.txt", p_mode, encoding="utf-8", errors="ignore")
    return TxtFile
# End def

    参数p_mode的选择:

  1. w:覆写
  2. r:读取
  3. a:附加

1.4 split()与IO的遍历

for row in testFile:
    rowList = row.split()
# End for

    在上述遍历中,testFile类型为IO,即直接使用open()打开的文件,row为string。此时对它按行遍历可以使用split()方法,返回list,其中添加需要的分割标识符即可,默认为空格。

二、正则表达式

    首先导入re库。

if (re.match('^[-+]?[0-9]+\.[0-9]+$', TC_input)) :
    print("输入错误,必须输入整数!")
    F_ReInput = True
elif (re.match('^[-+]?[0-9]+', TC_input)) :
    F_ReInput = False
    F_Cancel = False
else :
    print("输入格式错误,结束程序!")
    F_Cancel = True
#End if

    re.match()返回Ture|False,第一参数为正则表达式,第二参数为任意str。

三、显示输出

3.1 居中对齐

    使用str.center()

for i in range(0, 10) :
    Tri = '*' * (2 * i + 1)
    # 第i行的*号
    print(Tri.center(40, ' '))
#End for

    str.center()第一参数为距离(字符数量),第二参数为填充字符。

3.2 ASCII字符转换

  • ord(),ASCII字符->ASCII值
  • chr(),ASCII值->ASCII字符

    下面请看一段例题:凯撒密码,将字母顺移3个字符。

Ori = input('Input orignal string: \n')
# 输入

Ori = list(Ori)
IdxU = ord('A') + 23
IdxL = ord('a') + 23
#预处理

for i in range(len(Ori)) :
    if ord(Ori[i]) / IdxU < 1 and int(ord(Ori[i])) in range(ord('A'), ord("Z") + 1):
        Ori[i] = chr(ord(Ori[i]) + 3)
    elif ord(Ori[i]) / IdxU >= 1 and ord(Ori[i]) in range(ord('A'), ord("Z") + 1):
        Ori[i] = chr(ord(Ori[i]) - 23)
    elif ord(Ori[i]) / IdxL < 1 and ord(Ori[i]) in range(ord('a'), ord("z") + 1):
        Ori[i] = chr(ord(Ori[i]) + 3)
    elif ord(Ori[i]) / IdxL >= 1 and ord(Ori[i]) in range(ord('a'), ord("z") + 1):
        Ori[i] = chr(ord(Ori[i]) - 23)
    else :
        continue
    #End if
#End for

print(Ori)

3.3 保留小数

·1:print(“%.2f” % a)
·2:round(a, 2)

a = 12.345

print("%.2f" % a)
print(round(a, 2))

    这两种方法都是四舍五入的,若要使用去尾法或进一法则对其±”0.5″再四舍五入即可。

3.4 format

    下面看一段代码(统计字符串类型)演示format的使用方法。

inStr = input('Input string\n')

numCh = 0
numNu = 0
numSp = 0
numOt = 0
# ans initialize

for i in range(len(inStr)) :
    if ('a' <= inStr[i] and inStr <= 'z') or ('A' <= inStr[i] and inStr <= 'Z') :
        numCh += 1
    elif '0' <= inStr[i] and inStr <= '9' :
        numNu += 1
    elif inStr[i] == ' ' :
        numSp += 1
    else :
        numOt += 1
    # end if
# end for

print("ch : '{0}', nu : '{1}', sp : '{2}', ot : '{3}'".format(numCh, numNu, numSp, numOt))

三、数据结构

3.1 列表

  1. 声明:list = [ele, ele]
  2. 访问:list[idx],注意idx为int

    下面通过for in range实现快速构造列表:

CodeList = []
for i in range(ord('a'), ord('z') + 1):
    CodeList.append(chr(i))
# End for
  1. 排序:list.sort()
NumList.sort(reverse=True)
# 默认升序,reverse=Ture为降序

###########################################

# 获取列表的第二个元素
def takeSecond(elem):
    return elem[1]
# End def

# 列表
random = [(2, 2), (3, 4), (4, 1), (1, 3)]

# 指定第二个元素排序
random.sort(key = takeSecond)

3.2 字典

  1. 构造:dict = {ele, ele}
  2. 访问:dict[idx],注意idx为ele.first元素类型

    下面是一段演示:

NumDict = {1:"One", 2:"Two", 3:"Three", 4:"Four", 5:"Five", 6:"Six", 7:"Seven", 8:"Eight", 9:"Nine", 0:"Zero"}

inStr = input('input your phone num: ')

for i in range(len(inStr)):
    if (i != len(inStr)):
        print(NumDict[int(inStr[i])], end=' ')
    else:
        print(NumDict[int(inStr[i])], end='\n')
    # End if
# End for

3.3 综合应用

    下面是一个学生成绩表,求其综合成绩 = 期中0.5 + 期末0.5,并按综合成绩降序排序。
姓名,期中,期末
张三,69,95
李四,55,85
王五,79,85
宋海源,59,60

import csv

# 求综合成绩(0.5, 0.5),降序排序

# 读取文件
def OpenCsv(p_mode):
    refile = open('score.csv', p_mode, encoding = 'utf-8', errors = 'ignore')
    return refile
# End def

# 创建拷贝文件
def OpenCopy(p_mode):
    refile = open('score_copy.csv', p_mode, encoding = 'utf-8', errors = 'ignore')
    return refile
# End def

#  遍历求值,数据写入文件副本
def Taverser():
    csvFile = OpenCsv('r')
    csvRead = csv.DictReader(csvFile)

    csvCopy = OpenCopy('w')
    csvCopy = OpenCopy('a')
    # 清空、加入

    csvCopyHeader = ("姓名", "期中", "期末", "综合")
    csvCopy.write("{0},{1},{2},{3}\n".format(csvCopyHeader[0], csvCopyHeader[1], csvCopyHeader[2], csvCopyHeader[3]))

    for row in csvRead:
        totalScore = (int(row["期中"]) * 0.5) + (int(row["期末"]) * 0.5)
        csvCopy.write("{0},{1},{2},{3}\n".format(row["姓名"], row["期中"], row["期末"], totalScore))
    # End for
# End def

# 获取tuple元素
def GetTotal(ele):
    return float(ele[3])
# End def

# 排序
def CsvSort():
    csvCopy = OpenCopy('r')
    csvCopyRead = csv.DictReader(csvCopy)

    CopyList = []

    for row in csvCopyRead:
        CopyList.append((row["姓名"], row["期中"], row["期末"], row["综合"]))
    # End for

    CopyList.sort(key = GetTotal, reverse = True)

    return CopyList
# End def

# 覆写
def Overwrite():
    TargetFile = OpenCsv('w')
    TargetFile = OpenCsv('a')
    CopyList = CsvSort()

    csvCopyHeader = ("姓名", "期中", "期末", "综合")
    TargetFile.write("{0},{1},{2},{3}\n".format(csvCopyHeader[0], csvCopyHeader[1], csvCopyHeader[2], csvCopyHeader[3]))

    for ele in CopyList:
        TargetFile.write("{0},{1},{2},{3}\n".format(ele[0], ele[1], ele[2], ele[3]))
    # End for
# End def

def main():
    Taverser()
    Overwrite()
# End def

main()

四、jieba库

import jieba

def takeSecond(elem):
 return elem[1]

# 主文本
path = "三国演义.txt"
file = open(path, "r", encoding="utf-8")
text = file.read()
file.close()

words = jieba.lcut(text)
counts = {}
# 单词统计
for word in words:
    if len(word) == 1:
        continue
    elif ((word == "孔明") or (word == "孔明曰") or (word == "诸葛亮")):
        reword = "诸葛亮"
    elif ((word == "刘备") or (word == "玄德曰") or (word == "先主") or (word == "玄德")):
        reword = "刘备"
    elif ((word == "云长") or (word == "关羽") or (word == "关公")):
        reword = "关羽"
    elif ((word == "后主") or (word == "刘禅")):
        reword == "刘禅"
    else:
        reword = word
    # End if

    counts[reword] = counts.get(reword, 0) + 1
# End for

# 排除项
excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此",
"商议", "主公", "如何", "军士", "左右", "军马", "引兵", "大喜", "次日", "天下",
"东吴", "于是", "今日", "不敢", "魏兵", "一人", "都督", "人马", "陛下", "不知",
"汉中", "只见", "众将", "上马", "大叫", "蜀兵", "太守", "此人", "夫人", "后人",
"天子", "一面", "何不", "大军", "忽报", "背后", "城中", "先生", "何故", "百姓",
"然后", "先锋", "不如", "赶来", "令人", "成都", "原来", "江东", "下马", "正是",
"忽然", "喊声", "徐州", "因此", "不见", "未知", "大败", "大事", "之后", "引兵",
"起兵", "军中", "一军", "引军", "接应", "进兵", "大惊", "可以", "以为", "大怒",
"不得", "心中", "下文", "一声", "追赶", "粮草", "曹兵", "回报", "分解", "一齐"}

for delWord in excludes:
    try:
        del counts[delWord]
    except:
        continue
    # End try
# End for

# 统计
items = list(counts.items())
items.sort(key = takeSecond, reverse=True)

for i in range(20):
    item = items[i]
    keyWord = item[0]
    count = item[1]
    print("{0:<10}{1:>5}".format(keyWord, count))
# End for

五、图形界面

import tkinter as tk
import tkinter.messagebox

########## Section 00 : Function ##########

# 打开密码文件
def OpenFile(p_mode):
    TxtFile = open("pwd.txt", p_mode, encoding="utf-8", errors="ignore")
    return TxtFile
# End def

# 查询以存在用户及其密码
def SearchUser(p_user, p_pwd):
    Ruser = False
    Rpwd = False
    # return value  用户是否存在、密码是否错误

    pwdFile = OpenFile('r')
    # 打开文件

    # Search
    for row in pwdFile:
        rowList = row.split()
        if (rowList[0] == p_user):
            Ruser = True
            if (rowList[1] == p_pwd):
                Rpwd = True
                break
            # End if
        # End if
    # End for

    return [Ruser, Rpwd]
# End def

# 写入密码
def SetUser(p_user, p_pwd):
    pwdFile = OpenFile('a')

    pwdFile.write(p_user + " " + p_pwd + "\n")
# End def

# 登录函数
def Login(p_user, p_pwd):
    re = True

    searchAns = SearchUser(p_user, p_pwd)

    if (searchAns[0] == False):
        tk.messagebox.showerror(title = 'Error', message = 'User no Exists')
        re = False
    elif (searchAns[1] == False):
        tk.messagebox.showerror(title = 'Error', message = 'Wrong password')
        re = False
    else:
        tk.messagebox.showinfo(title = 'Success', message = 'Login Success')
    # End if

    return re
# End def

def Registe(p_a, p_b):
    SetUser(p_a, p_b)
    # 抛出Message
    tk.messagebox.showinfo(title = 'Registe', message = 'Registe Success')
# End def

# 注册函数
def Register():
    rWin = tk.Tk()
    # 注册窗口

    rWin.title("Registe")
    rWin.geometry("300x300")

    # 注册Entry
    rEntryU = tk.Entry(rWin, show = None)
    rEntryP = tk.Entry(rWin, show = '*')
    rEntryU.pack()
    rEntryP.pack()

    # 注册按钮
    rButton = tk.Button(rWin, text = 'Registe', command = lambda: Registe(rEntryU.get(), rEntryP.get()))
    rButton.pack()

    # 窗口循环
    rWin.mainloop()
# End def

########## Section 00 : Window ##########
def Win():
    mainWin = tk.Tk()
    # 主窗口

    mainWin.title("Login")
    mainWin.geometry('500x500')

    # 用户名与密码的输入
    mainEntryU = tk.Entry(mainWin, show = None)
    mainEntryP = tk.Entry(mainWin, show = '*')
    mainEntryU.pack()
    mainEntryP.pack()

    # 登录与注册按钮
    mainButtonL = tk.Button(mainWin, text = 'Login', command = lambda: Login(mainEntryU.get(), mainEntryP.get()))
    mainButtonR = tk.Button(mainWin, text = 'Registe', command = Register)
    mainButtonL.pack()
    mainButtonR.pack()

    mainWin.mainloop()
# End def

def main():
    Win()
# End def

main()