从零开始的python之旅(day4)
昨天博客园好像崩了,所以昨天晚上没写,就挪到今天来补了,昨天主要是文件操作,话不多说,上代码
addressBook
def main():file1 = open('TeleAddressBook.txt','rb')file2 = open('EmailAddressBook.txt','rb')file1.readline()file2.readline()list1 = file1.readlines()list2 = file2.readlines()# print(list1)list1_name = []list1_tele = []list2_name = []list2_email = []for i in list1:el = i.split()list1_name.append(str(el[0].decode('gbk')))list1_tele.append(str(el[1].decode('gbk')))for i in list2:el = i.split()list2_name.append(str(el[0].decode('gbk')))list2_email.append(str(el[1].decode('gbk')))list3 = []list3.append('姓名\t 电话 \t 邮箱\n')for i in range(len(list1_name)):s = ''if list1_name[i] in list2_name:j = list2_name.index(list1_name[i])s = '\t'.join([list1_name[i],list1_tele[i],list2_email[j]])else :s = '\t'.join([list1_name[i],list1_tele[i],str(' ----- ')])s += '\n'list3.append(s)for i in range(len(list2_name)) :s = ''if list2_name[i] not in list1_name:s = '\t'.join([list2_name[i],str(' ----- '),list2_email[i]])s += '\n'list3.append(s)file3 = open('addressbook.txt','w')file3.writelines(list3)file1.close()file2.close()file3.close()if __name__ == '__main__':main()
这段程序主要是将两个文件合并,在此处没啥问题,可以看看day5差不多类似代码,换种方法读入
CountOccurencesOfWords
import turtlecount = 10
data = []
words = []
yScale = 12
xScale = 35
def drawLine(p,x1,y1,x2,y2):p.penup()p.goto(x1,y1)p.pendown()p.goto(x2,y2)def drawText(p,x,y,text):p.penup()p.goto(x,y)p.pendown()p.write(text)def drawRectangle(p,x,y):x = x*xScaley = y*yScaledrawLine(p,x-5,0,x-5,y)drawLine(p,x-5,y,x+5,y)drawLine(p,x+5,y,x+5,0)drawLine(p,x+5,0,x-5,0)def drawBar(p):for i in range(count):drawRectangle(p,i+1,data[i])def drawGraph(p):drawLine(p,0,0,360,0)drawLine(p,0,300,0,0)for x in range(1,count+1):drawText(p,x*xScale-4,-20,(words[x - 1]))drawText(p,x*xScale-4,data[x-1]*yScale+10,data[x-1])drawBar(p)def replacePunctuations(line):for ch in line:if ch in '~@#$%^&*()_+=<>?/.,;:{}[]|\"""':line = line.replace(ch," ")return linedef processLine(line,wordCounts):line = replacePunctuations(line)words = line.split()for word in words:if word in wordCounts:wordCounts[word] += 1else :wordCounts[word] = 1def main():file = open('article.txt','r',encoding='utf-8')wordCounts = {}for i in file:processLine(i.lower(),wordCounts)pairs = list(wordCounts.items())items = [[x,y] for (y,x) in pairs]items.sort()for i in range(len(items) - 1,len(items) - count - 1,-1):print(items[i][1] + '\t' + str(items[i][0]))data.append(items[i][0])words.append(items[i][1])file.close()turtle.title('词频结果柱状图')turtle.setup(900,750,0,0)p = turtle.Turtle()p.hideturtle()p.width(3)drawGraph(p)if __name__ == '__main__':main()
在这段程序中,读取文章内容(英文)然后转化大小写等等一系列操作后,统计单词出现频率然后利用turtle画出来。
drawDay
import turtle
from turtle import Turtle
rulues = {0: [1, 1, 1, 0, 1, 1, 1],1: [0, 0, 1, 0, 0, 0, 1],2: [0, 1, 1, 1, 1, 1, 0],3: [0, 1, 1, 1, 0, 1, 1],4: [1, 0, 1, 1, 0, 0, 1],5: [1, 1, 0, 1, 0, 1, 1],6: [1, 1, 0, 1, 1, 1, 1],7: [0, 1, 1, 0, 0, 0, 1],8: [1, 1, 1, 1, 1, 1, 1],9: [1, 1, 1, 1, 0, 0, 1]
}
def makedraw(p,x):#可以利用规律减少代码量,半欧拉图,从左侧中部开始画pending = rulues.get(x)p.penup()if(pending[0]):p.pendown()p.fd(50)p.right(90)p.penup()if (pending[1]):p.pendown()p.fd(50)p.right(90)p.penup()if (pending[2]):p.pendown()p.fd(50)p.penup()if (pending[3]):p.pendown()p.right(90)p.fd(50)p.penup()if (pending[4]):p.pendown()p.left(90)p.fd(50)p.penup()if (pending[5]):p.pendown()p.left(90)p.fd(50)p.penup()if (pending[6]):p.pendown()p.left(90)p.fd(50)p.penup()def draw(x):turtle.setworldcoordinates(-20,-500,700,500)p = Turtle()p.pensize(5)p.hideturtle()p.color('red')p.speed(5)p.penup()p.left(90)xx = int(-70)for i in range(len(x)):xx += 80p.goto(xx,0)if i == 4:p.goto(xx-15,0)p.write('年', font=("Arial", 15, "normal"))p.goto(xx+15,0)p.color('green')elif i == 6:p.goto(xx-15,0)p.write('月', font=("Arial", 15, "normal"))p.goto(xx+15,0)p.color('blue')makedraw(p,int(x[i]))xx += 80p.goto(xx - 15, 0)p.write('日', font=("Arial", 15, "normal"))p.goto(xx + 15, 0)turtle.done()
def main():yearmonthday = input('输入年月日(例如20250101,如不满八位,用0补满八位)\n')draw(yearmonthday)main()
这一部分则是昨天作业,
route
import turtledef main():turtle.title('数据驱动的动态路径绘制')turtle.setup(800,600,0,0)p = turtle.Turtle()p.color('red')p.width(5)p.shape('turtle')p.speed(5)result = []file = open('data.txt','r')for i in file:result.append(list(map(float,i.split(','))))print(result)for i in range(len(result)):p.color(result[i][3:6])p.fd(result[i][0])if result[i][1] :p.right(result[i][2])else :p.left(result[i][2])p.goto(0,0)file.close()if __name__ == '__main__':main()
day4主要是,文件处理和turtle库的进阶。文件处理时,还是要注意,encoding和decode的类型类型选择。其他的没啥了