Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d129274
Create Liez‘s Python Exercise
llapuras Apr 23, 2016
eeaf928
Delete Liez‘s Python Exercise
llapuras Apr 23, 2016
a813525
Added files via upload
llapuras Apr 23, 2016
a0cef69
Delete Liez-Python-Exercise-master.zip
llapuras Apr 23, 2016
009abfc
Create Liez
llapuras Apr 23, 2016
36e0e50
Added files via upload
llapuras Apr 23, 2016
d53c5b9
Create 0000.py
llapuras Apr 23, 2016
9fdb627
Added files via upload
llapuras Apr 23, 2016
c6102d1
Create 0001.py
llapuras Apr 23, 2016
5a69663
Added files via upload
llapuras Apr 23, 2016
73ed006
Delete 0001.py
llapuras Apr 23, 2016
c563bcb
Delete output_sample
llapuras Apr 23, 2016
e99ea87
Create 0001.py
llapuras Apr 23, 2016
6f44dac
Delete README.md
llapuras Apr 23, 2016
1596754
Added files via upload
llapuras Apr 23, 2016
911eaa5
Create 0002.py
llapuras Apr 23, 2016
d2cdf28
Create 0004.py
llapuras Apr 23, 2016
6e1144f
Added files via upload
llapuras Apr 23, 2016
89c4a25
Create 0005.py
llapuras Apr 23, 2016
2ee454c
Create 0010.py
llapuras Apr 23, 2016
bb82440
Added files via upload
llapuras Apr 23, 2016
8005408
Create 0011.py
llapuras Apr 23, 2016
845a416
Added files via upload
llapuras Apr 23, 2016
87622ad
Create 0012.py
llapuras Apr 23, 2016
340d901
Added files via upload
llapuras Apr 23, 2016
7ebded0
Create 0013.py
llapuras Apr 23, 2016
757db46
Create 0014.py
llapuras Apr 24, 2016
a04a259
Create student.txt
llapuras Apr 24, 2016
8483868
Create 0015.py
llapuras Apr 24, 2016
2f21a62
Create student.txt
llapuras Apr 24, 2016
a27e38d
Create 0017.py
llapuras Apr 24, 2016
662884b
Create 0020.py
llapuras Apr 24, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Liez-python-code/0000/0000.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

from PIL import Image, ImageDraw, ImageFont

def add_num():
im = Image.open('in.jpg')
xsize, ysize = im.size
draw = ImageDraw.Draw(im)
font = ImageFont.truetype("arial.ttf", xsize // 3)
draw.text((ysize // 5 * 4, 0), '3', (250,128,114), font)
im.save('out.jpg')

add_num()
Binary file added Liez-python-code/0000/in.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Liez-python-code/0000/out.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions Liez-python-code/0001/0001.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# coding = utf-8
__author__= 'liez'

import random

def make_number(num, length):
str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
a = []
i = 0
while i < num:
numstr = ''
for j in range(length):
numstr += random.choice(str)
if numstr not in a: #如果没重复
a.append(numstr)
i += 1
print(numstr)

make_number(20,10)
22 changes: 22 additions & 0 deletions Liez-python-code/0001/output_sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
COsSx9umWf
QKMSbDVj0G
ARdycodYAf
f8h0drSHJl
e0T6lF6aO1
ZB06xAlSnf
SmcAqxYHqm
209me2lsCl
NnvmMeMgqd
FqxLZVlzpC
dj7luHI53s
RytNv7yhTH
bg7PDFmGE1
I7r7S1mpWK
agN2PYF3TI
523YX6TdS8
GmffcmyoYX
MmZZ2pHieO
gaHTmOVPDT
6rgb3v2TJP


35 changes: 35 additions & 0 deletions Liez-python-code/0002/0002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# coding = utf-8
__author__= 'liez'

import random
import sqlite3

def make_number(num, length):
str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
a = []
i = 0
while i < num:
numstr = ''
for j in range(length):
numstr += random.choice(str)
if numstr not in a: #如果没重复
a.append(numstr)
i += 1
print(a)
return a

def save(a):
try:
connect = sqlite3.connect('codelist.db')
except:
print("failed")
cur = connect.cursor()
cur.execute('create table if not exists codes(code char(20) primary key)')
for item in a:
cur.execute('insert into codes values (?)', [item])
print("success")
connect.commit()
cur.close()
connect.close()

save(make_number(20, 10))
20 changes: 20 additions & 0 deletions Liez-python-code/0004/0004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# coding = utf-8
__author__= 'liez'

import re
def num(path):
with open(path, 'r') as file:
data=file.read()
print(data)
words=re.compile('[a-zA-Z0-9]+') #compile好像是必须用的,用来格式转换什么的,然后才能进行匹配之类的操作
dict={}

for x in words.findall(data):
if x not in dict:
dict[x]=1
else:
dict[x]+=1

print(dict)

num('liez.txt')
5 changes: 5 additions & 0 deletions Liez-python-code/0004/output_sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
I am liez.
I am a player.
I love programming.
{'love': 1, 'I': 3, 'player': 1, 'programming': 1, 'a': 1, 'am': 2, 'liez': 1}

22 changes: 22 additions & 0 deletions Liez-python-code/0005/0005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from PIL import Image
import glob, os

def resize():
for files in glob.glob('*.jpg'):
filepath,filename = os.path.split(files) #分割文件名和路径名
fname,fext = os.path.splitext(filename)
im = Image.open(files)
w,h = im.size
if w > 640:
x = w/640.0
w = 640
h = int(h/x)
if h>1136:
x = h/1136.0
h = 1136
w = int(w/x)
print(w, h)
im0 = im.resize((w,h),Image.ANTIALIAS)
im0.save('0005'+filename)

resize()
35 changes: 35 additions & 0 deletions Liez-python-code/0010/0010.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# codeing: utf-8

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import string
import random


def get4char():
return [random.choice(string.ascii_letters) for _ in range(4)] # 可以把‘_’换做任意字母,‘_’说明后续不用


def getcolor():
return random.randint(30, 100), random.randint(30, 100), random.randint(30, 100)


def getpicture():
width = 240
height = 60

image = Image.new('RGB', (width, height), (180, 180, 180))
font = ImageFont.truetype('arial.ttf', 40)

draw = ImageDraw.Draw(image)
code = get4char()
for ch in range(4):
draw.text((60 * ch,30), code[ch], font=font, fill=getcolor())

for _ in range(random.randint(1500, 3000)):
draw.point((random.randint(0, width), random.randint(0, height)), fill=getcolor())

image = image.filter(ImageFilter.BLUR)
image.save("".join(code) + '.jpg')


getpicture()
Binary file added Liez-python-code/0010/sample1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Liez-python-code/0010/sample2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Liez-python-code/0010/sample3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions Liez-python-code/0011/0011.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def filterwords(x):
with open(x, 'r') as f:
text = f.read()
print (text.split('\n'))
userinput = input('myinput:')
for i in text.split('\n'):
if i in userinput:
return True

if filterwords('word.txt'):
print ('freedom')
else:
print ('human rights')
11 changes: 11 additions & 0 deletions Liez-python-code/0011/word.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
10 changes: 10 additions & 0 deletions Liez-python-code/0012/0012.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def filtertext(x):
with open(x, 'r') as f:
text = f.read()
userinput = input('myinput:')
for i in text.split('\n'):
if i in userinput:
userinput = userinput.replace(str(i), '*'*len(i))
print(userinput)

filtertext('word.txt')
2 changes: 2 additions & 0 deletions Liez-python-code/0012/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
myinput:我是住在北京的程序员。
我是住在**的***。
19 changes: 19 additions & 0 deletions Liez-python-code/0013/0013.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 网址:http://tieba.baidu.com/p/4341640851

import os
import re
import urllib.request

def pic_collector(url):
content = urllib.request.urlopen(url).read()
r = re.compile('<img class="BDE_Image" pic_type="1" width="450" height="450" src="(.*?)" ')
pic_list = r.findall(content.decode('utf-8'))

os.mkdir('pic_collection')
os.chdir(os.path.join(os.getcwd(), 'pic_collection'))
for i in range(len(pic_list)):
pic_num = str(i) + '.jpg'
urllib.request.urlretrieve(pic_list[i], pic_num)
print("success!" + pic_list[i])

pic_collector("http://tieba.baidu.com/p/4341640851")
18 changes: 18 additions & 0 deletions Liez-python-code/0014/0014.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import re
import xlwt

def read2xls(x):
datatable = xlwt.Workbook(encoding = 'utf-8', style_compression = 0)
newsheet = datatable.add_sheet('student', cell_overwrite_ok = True)
num = 0
with open(x, 'r') as f:
text = f.read()
info = re.compile(r'"(\d+)":\["(.*?)",(\d+),(\d+),(\d+)]')
for x in info.findall(text):
for i in range(len(x)):
newsheet.write(num, i, x[i])
num += 1

datatable.save('liez.xls')

read2xls('student.txt')
5 changes: 5 additions & 0 deletions Liez-python-code/0014/student.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}
18 changes: 18 additions & 0 deletions Liez-python-code/0015/0015.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import re
import xlwt

def read2xls(x):
datatable = xlwt.Workbook(encoding = 'utf-8', style_compression = 0)
newsheet = datatable.add_sheet('student', cell_overwrite_ok = True)
num = 0
with open(x, 'r') as f:
text = f.read()
info = re.compile(r'"(\d+)" : "(.*?)"')
for x in info.findall(text):
for i in range(len(x)):
newsheet.write(num, i, x[i])
num += 1

datatable.save('liez.xls')

read2xls('student.txt')
5 changes: 5 additions & 0 deletions Liez-python-code/0015/student.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
34 changes: 34 additions & 0 deletions Liez-python-code/0017/0017.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import xlrd
import xml.dom.minidom as md


def get_xls_data(filename):
book = xlrd.open_workbook(filename)
sheet = book.sheet_by_index(0)
content = {}
for i in range(sheet.nrows):
content[i+1] = sheet.row_values(i)[1:]
return content


def write_to_xml(xlscontent):

xmlfile = md.Document()  #创建新xml文件

root = xmlfile.createElement('root')  #创建节点
students = xmlfile.createElement('students')  #创建节点

xmlfile.appendChild(root)  #在文件中添加root节点
root.appendChild(students)  #在root下添加students节点

comment = xmlfile.createComment('学生信息表 "id" : [名字, 数学, 语文, 英文]')  #创建评论
students.appendChild(comment)  #在students标签下添加comment

xmlcontent = xmlfile.createTextNode(str(xlscontent))    #创建文本节点
students.appendChild(xmlcontent)  在students标签下添加文本内容

with open('students.xml', 'wb') as f:
f.write(xmlfile.toprettyxml(encoding = 'utf-8'))  #写入文件


write_to_xml(get_xls_data('students.xls'))
Loading