Skip to content

add 0009 0014 0015 0016 #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions monkey/0008/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding:utf-8 -*-

'''

第 0008 题:一个HTML文件,找出里面的正文。

@Author monkey
@Date 2017-8-31
'''
import json

from bs4 import BeautifulSoup


def findContent():
path = "The world's leading software development platform · GitHub.html"

with open(path, encoding='UTF-8') as file:
soup = BeautifulSoup(file)

# print(soup.prettify())
print(soup.body)



if __name__ == '__main__':
findContent()

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions monkey/0009/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding:utf-8 -*-

'''

第 0009 题:一个HTML文件,找出里面的链接。

@Author monkey
@Date 2017-8-31
'''
import json

from bs4 import BeautifulSoup


def findTagA():
path = "The world's leading software development platform · GitHub.html"

with open(path, encoding='UTF-8') as file:
soup = BeautifulSoup(file)

# print(soup.prettify())
links = []
for i in soup.find_all('a'):
links.append(i['href'])

print(links)

if __name__ == '__main__':
findTagA()
56 changes: 56 additions & 0 deletions monkey/0014/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding:utf-8 -*-

'''

第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}
请将上述内容写到 student.xls 文件中。

@Author monkey
@Date 2017-8-31
'''
import json
import xlwt

def getStudent():

with open('student.txt', 'r', encoding = 'UTF-8') as file:
text = ''
for line in file:
text = text + line

stu_json = json.loads(text, encoding = 'UTF-8')

print(stu_json)

writeInXLS(stu_json)


def writeInXLS(dict):
fileName = 'student.xls'
# 创建 xls 文件
file = xlwt.Workbook(encoding = 'utf-8')
# 创建 表
sheet = file.add_sheet('student', cell_overwrite_ok=True)

row = 0
col = 0

for k, v in sorted(dict.items(), key=lambda d:d[0]):
sheet.write(row, col, k)
for i in v:
col += 1
sheet.write(row, col, i)

row += 1
col = 0

file.save(fileName)
print('写入成功')

if __name__ == '__main__':
getStudent()
5 changes: 5 additions & 0 deletions monkey/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]
}
Binary file added monkey/0014/student.xls
Binary file not shown.
5 changes: 5 additions & 0 deletions monkey/0015/city.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
Binary file added monkey/0015/city.xls
Binary file not shown.
54 changes: 54 additions & 0 deletions monkey/0015/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding:utf-8 -*-

'''
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:

{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
请将上述内容写到 city.xls 文件中,如下图所示:

@Author monkey
@Date 2017-8-31
'''
import json
import xlwt

def getCity():

with open('city.txt', 'r', encoding='UTF-8') as file:
text = ''
for line in file:
text = text + line

city_json = json.loads(text, encoding = 'UTF-8')
print(city_json)

writeInXLS(city_json)


def writeInXLS(dict):
fileName = 'city.xls'

# 创建 文件
file = xlwt.Workbook()
# 创建 表
sheet = file.add_sheet('city', cell_overwrite_ok=True)

row = 0
col = 0

for k, v in sorted(dict.items(), key=lambda d:d[0]):
sheet.write(row, col, k)
col += 1
sheet.write(row, col, v)

row += 1
col = 0

file.save(fileName)

if __name__ == '__main__':
getCity()
55 changes: 55 additions & 0 deletions monkey/0016/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding:utf-8 -*-

'''
纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:

[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]
请将上述内容写到 numbers.xls 文件中,如下图所示:

@Author monkey
@Date 2017-8-31
'''
import json
import xlwt

def getNumber():

with open('numbers.txt', 'r', encoding='UTF-8') as file:
text = ''
for line in file:
text = text + line

number_json = json.loads(text, encoding = 'UTF-8')
print(number_json)

writeInXLS(number_json)


def writeInXLS(list):
fileName = 'numbers.xls'

# 创建 文件
file = xlwt.Workbook()
# 创建 表
sheet = file.add_sheet('numbers', cell_overwrite_ok=True)

row = 0
col = 0


for l in list:
for i in l:
sheet.write(row, col, i)
col += 1

row += 1
col = 0

file.save(fileName)

if __name__ == '__main__':
getNumber()
5 changes: 5 additions & 0 deletions monkey/0016/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]
Binary file added monkey/0016/numbers.xls
Binary file not shown.