diff --git a/01/01.py b/01/01.py new file mode 100644 index 0000000000..30311c187f --- /dev/null +++ b/01/01.py @@ -0,0 +1,18 @@ +# -*- coding: UTF-8 -*- +from PIL import Image +from PIL import ImageDraw,ImageFont + + + + +im=Image.open("./qq.png") +name="4" +print(im.format,im.size,im.mode) +ps=ImageDraw.Draw(im) +box=im.size +ps.ink= 255 + 0 * 256 + 0 * 256 * 256 +ps.text((im.size[0]-35,5),name,font=ImageFont.truetype("C:\Windows\Fonts\simfang.ttf",50)) + +im.show() +im.save("./qq1.png") + diff --git a/01/qq.png b/01/qq.png new file mode 100644 index 0000000000..50e348b270 Binary files /dev/null and b/01/qq.png differ diff --git a/01/qq1.png b/01/qq1.png new file mode 100644 index 0000000000..f9c181d1ed Binary files /dev/null and b/01/qq1.png differ diff --git a/02/02 b/02/02 new file mode 100644 index 0000000000..2e83544956 --- /dev/null +++ b/02/02 @@ -0,0 +1 @@ +adsf diff --git a/02/02.py b/02/02.py new file mode 100644 index 0000000000..4850ee3519 --- /dev/null +++ b/02/02.py @@ -0,0 +1,10 @@ +import random +coupon=[] + +for i in range(200): + coupon.append(random.randint(10000000,99999999)) + + +print(coupon) + + diff --git a/03/03 b/03/03 new file mode 100644 index 0000000000..a49f4dd3a4 --- /dev/null +++ b/03/03 @@ -0,0 +1,54 @@ +1.安装MYSQL + +2.安装install mysql-connector #安装驱动或者pip install mysql-connector-python --allow-external mysql-connector-python + +3.测试 +# 导入MySQL驱动: +>>>import mysql.connector +#连接dexterliao +conn = mysql.connector.connect(user='root', password='', database='dexterliao') +cursor = conn.cursor() +#创建user表单 +cursor.execute('create table user (`id` INT UNSIGNED AUTO_INCREMENT, `code` VARCHAR(40) NOT NULL,PRIMARY KEY ( `id` ))') + +4.安装SQLAlchemy + +5.底层处理 +from sqlalchemy import create_engine +mysql+mysqlconnector://:@[:]/ +engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test') +6.创建表 +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index +from sqlalchemy.orm import sessionmaker, relationship +from sqlalchemy import create_engine + + +engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test') + +# 创建对象的基类: +Base = declarative_base() + +# 定义User对象: +class User(Base): + # 表的名字: + __tablename__ = 'user' + + # 表的结构: + id = Column(String(20), primary_key=True) + code = Column(String(20)) +# 创建DBSession类型: +DBSession = sessionmaker(bind=engine) + + +# 创建session对象: +session = DBSession() +# 创建新User对象: +new_user = User(id='5', name='Bob') +# 添加到session: +session.add(new_user) +# 提交即保存到数据库: +session.commit() +# 关闭session: +session.close() + diff --git a/03/03.py b/03/03.py new file mode 100644 index 0000000000..4b8f29bea4 --- /dev/null +++ b/03/03.py @@ -0,0 +1,38 @@ +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index +from sqlalchemy.orm import sessionmaker, relationship +from sqlalchemy import create_engine +import random +import mysql.connector +#连接dexterliao +conn = mysql.connector.connect(user='root', password='', database='dexterliao') +cursor = conn.cursor() +#创建user表单 +cursor.execute('create table user (`id` INT UNSIGNED AUTO_INCREMENT, `code` VARCHAR(40) NOT NULL,PRIMARY KEY ( `id` ))') +#sqlalchemy 连接mysql +engine = create_engine('mysql+mysqlconnector://root:@localhost:3306/dexterliao') + +# 创建对象的基类: +Base = declarative_base() + +# 定义User对象: +class User(Base): + # 表的名字: + __tablename__ = 'user' + + # 表的结构: + id = Column(Integer, primary_key=True) + code = Column(String(40)) +# 创建DBSession类型: +DBSession = sessionmaker(bind=engine) +# 创建session对象: +session = DBSession() +# 创建新User对象: +for i in range(200): + new_code = User(id=(i+1), code=random.randint(10000000,99999999)) + session.add(new_code) + + +session.commit() +session.close() + diff --git a/03/QUERY.py b/03/QUERY.py new file mode 100644 index 0000000000..8653d4b3d2 --- /dev/null +++ b/03/QUERY.py @@ -0,0 +1,30 @@ +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index +from sqlalchemy.orm import sessionmaker, relationship +from sqlalchemy import create_engine + + +#sqlalchemy 连接mysql +engine = create_engine('mysql+mysqlconnector://root:@localhost:3306/dexterliao') +# 创建对象的基类: +Base = declarative_base() +# 定义User对象: +class User(Base): + # 表的名字: + __tablename__ = 'user' + + # 表的结构: + id = Column(Integer, primary_key=True) + code = Column(String(40)) + +# 创建DBSession类型: +DBSession = sessionmaker(bind=engine) +# 创建Session: +session = DBSession() +# 创建Query查询,filter是where条件,最后调用one()返回唯一行,如果调用all()则返回所有行: +user = session.query(User).filter(User.id==5).one() +# 打印类型和对象的name属性: +print('type:', type(user)) +print('name:', user.code) +# 关闭Session: +session.close() diff --git a/05/05.py b/05/05.py new file mode 100644 index 0000000000..9e626d18c7 --- /dev/null +++ b/05/05.py @@ -0,0 +1,16 @@ +import re + +def count_the_words(path): + with open(path) as f: + text=f.read() + wordlist=re.findall(r'[a-zA-Z0-9]+', text) + print(wordlist) + count=len(wordlist) + + return count +if __name__ == '__main__': + nums = count_the_words('.\\file.txt') +print (nums) + + + diff --git a/05/file.txt b/05/file.txt new file mode 100644 index 0000000000..9d2f798318 --- /dev/null +++ b/05/file.txt @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# ** 0004 ⣺**һӢĵĴıļͳеĵʳֵĸ +import re + +fin = open('source/0004-text.txt', 'r') +str = fin.read() + +reObj = re.compile('\b?(\w+)\b?') +words = reObj.findall(str) + +wordDict = dict() + +for word in words: + if word.lower() in wordDict: + wordDict[word.lower()] += 1 + else: + wordDict[word] = 1 + +for key, value in wordDict.items(): +print('%s: %s' % (key, value)) \ No newline at end of file diff --git a/06/06 b/06/06 new file mode 100644 index 0000000000..7898192261 --- /dev/null +++ b/06/06 @@ -0,0 +1 @@ +a diff --git a/06/06.py b/06/06.py new file mode 100644 index 0000000000..4026a5b6d2 --- /dev/null +++ b/06/06.py @@ -0,0 +1,29 @@ +from PIL import Image +import os + + + +def Re_size(path): + iPhone5_WIDTH = 1136 + iPhone5_HEIGHT = 640 + new_path=r"\\new\\" + name_list=os.listdir(path) + for name in name_list: + photo=Image.open(path+name) + w, h = photo.size + print(name) + if w>iPhone5_WIDTH: + w=iPhone5_WIDTH + h=h*iPhone5_WIDTH//w + if h>iPhone5_HEIGHT: + h=iPhone5_HEIGHT + w=w*iPhone5_HEIGHT//h + resize_photo=photo.resize((w,h),Image.ANTIALIAS)# ANTIALIAS滤镜缩放 + resize_photo.save(os.getcwd()+new_path+name) + print(name) + + + + +if __name__ == '__main__': + Re_size(r".//photos//") diff --git a/06/test.py b/06/test.py new file mode 100644 index 0000000000..fa34ff9c95 --- /dev/null +++ b/06/test.py @@ -0,0 +1,8 @@ +from PIL import Image +import os +path='.//photos//' +name_list=os.listdir(path) +for name in name_list: + print(path+name) +print(name_list) +print(os.getcwd()) \ No newline at end of file diff --git a/07/01 b/07/01 new file mode 100644 index 0000000000..7898192261 --- /dev/null +++ b/07/01 @@ -0,0 +1 @@ +a diff --git a/07/07.py b/07/07.py new file mode 100644 index 0000000000..1f21ea434c --- /dev/null +++ b/07/07.py @@ -0,0 +1,32 @@ +import os +import re + +def get_txt(path): + f_path=[] + for root, dirs, files in os.walk(path): + print(root,"\n",dirs,"\n",files) + for f in files: + if f.lower().endswith(".txt"): + f_path.append(os.path.join(root,f)) + return f_path + +def text_analysis(f_path): + word_dic = {} + f=open(f_path) + text=f.read() + word_list=re.findall(r'[a-zA-Z]+',text) + print(word_list) + for w in word_list: + if w in word_dic: + word_dic[w]+=1 + else: + word_dic[w]= 1 + sort_wordic=sorted(word_dic.items(),key=lambda item:item[1]) + print(sort_wordic) + print("在文件%s中,关键词为%s,出现次数为%s"%(os.path.basename(f_path),sort_wordic[-1][0],sort_wordic[-1][1])) + +if __name__ == '__main__': + for file in get_txt(os.getcwd()): + text_analysis(file) + + diff --git a/07/file.txt b/07/file.txt new file mode 100644 index 0000000000..924f2f755e --- /dev/null +++ b/07/file.txt @@ -0,0 +1,33 @@ +""" + 0006 ⣺һĿ¼һµռǣ txtΪ˱ִʵ⣬ +ݶӢģͳƳΪÿƪռҪĴʡ +""" + +import os +import re + +def walk_dir(path): + file_path = [] + for root, dirs, files in os.walk(path): + for f in files: + if f.lower().endswith('txt'): + file_path.append(os.path.join(root, f)) + return file_path + +def find_key_word(filepath): + word_dic = {} + filename = os.path.basename(filepath) + with open(filepath) as f: + text = f.read() + words_list = re.findall(r'[A-Za-z]+', text.lower()) + for w in words_list: + if w in word_dic: + word_dic[w] += 1 + else: + word_dic[w] = 1 + sorted_word_list = sorted(word_dic.items(), key=lambda d: d[1]) + print u"%sļУ%sΪؼʣ%s" %(filename, sorted_word_list[-1][0], sorted_word_list[-1][1]) + +if __name__ == "__main__": + for file_path in walk_dir(os.getcwd()): +find_key_word(file_path) \ No newline at end of file diff --git a/08/08 b/08/08 new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/08/08 @@ -0,0 +1 @@ +1 diff --git a/08/08.py b/08/08.py new file mode 100644 index 0000000000..06eaa552ae --- /dev/null +++ b/08/08.py @@ -0,0 +1,40 @@ +#coding:utf-8 +import os + +def get_py(path): + f_path=[] + for root, dirs, files in os.walk(path): + print(root,"\n",dirs,"\n",files) + for f in files: + if f.lower().endswith(".py"): + f_path.append(os.path.join(root,f)) + return f_path +def py_analysis(py_path,x,y,z): + f=open(py_path,encoding='UTF-8') + f=f.read() + f=f.split("\n") + N_um=0 + zhushi_line=0 + emptyline=0 + for line in f: + N_um += 1 + if line.startswith('#'): + zhushi_line +=1 + if line.startswith('\'\'\''): + zhushi_line += 1 + if line.startswith('\"\"\"'): + zhushi_line += 1 + if line.startswith(''): + emptyline+=1 + print("在文件%s中,总数为%s行,注释行为%s,空行为%s" %(os.path.basename(py_path), N_um, zhushi_line, emptyline)) + return N_um,zhushi_line,emptyline +if __name__ == '__main__': + total_num=0 + total_zhushi_line=0 + total_emptyline=0 + for file in get_py(os.getcwd()): + b,c,d=py_analysis(file,x=0,y=0,z=0) + total_num += b + total_zhushi_line += c + total_emptyline += d + print("写入总数为%s行,注释行为%s,空行为%s" % (total_num, total_zhushi_line,total_emptyline)) diff --git a/08/test.py b/08/test.py new file mode 100644 index 0000000000..b55f7b58ae --- /dev/null +++ b/08/test.py @@ -0,0 +1,9 @@ +def secondvalue(a,b): + c=8 + return ("s","s",c) + + +x= secondvalue('s',2)[0] +y = secondvalue('s',2)[1] +z = secondvalue('s',2)[2] +print ('x:',x,'y:',y,'z:',z) \ No newline at end of file