chapter6 项目实践
注:第一道实践例题尚未调试成功,此处只贴第2、3道
# 第2道
# -*- coding:utf-8 -*-
# @FileName :chapter6_practice_project2.py
# @Time :2020/8/16 16:47
# @Author :Wu Hongyi
# @Copyright :Wu Hongyi
# Function description:
'''
本文件为Python编程快速上手-让繁琐工作自动化一书的第六章项目练习代码
'''
# @version :Version1.0
# Modification description:
'''
'''
# 项目:在Wiki标记中添加无序列表
# 创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号
# 从剪贴板中取得文本,在每一行开始处加上星号和空格,然后将这段新的文本贴回到剪贴板
# 待复制文本:
# Lists of animals
# Lists of aquarium life
# Lists of biologists by author abbreviation
# Lists of cultivars
# 运行程序后剪贴板内容输出格式:
# * Lists of animals
# * Lists of aquarium life
# * Lists of biologists by author abbreviation
# * Lists of cultivars
# 程序:
# bulletPointAdder.py
# 第1步:从剪贴板中复制和粘贴
#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.
import pyperclip
text = pyperclip.paste()
# TODO: Separate lines and add stars. # TODO注释是提醒,最后应该完成这部分程序
pyperclip.copy(text)
# 第2步:分离文本中的行,并添加星号
#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.
import pyperclip
text = pyperclip.paste()
# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)): # loop through all indexes in the "lines" list
lines[i] = '* ' + lines[i] # add star to each string in "lines" list
pyperclip.copy(text)
# 第3步:连接修改过的行
#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.
import pyperclip
text = pyperclip.paste()
# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)): # loop through all indexes for "lines" list
lines[i] = '* ' + lines[i] # add star to each string in "lines" list
text = '\n'.join(lines)
pyperclip.copy(text)
# 第3道
# -*- coding:utf-8 -*-
# @FileName :chapter6_practice_project3.py
# @Time :2020/8/16 17:16
# @Author :Wu Hongyi
# @Copyright :Wu Hongyi
# Function description:
'''
本文件为Python编程快速上手-让繁琐工作自动化一书的第六章实践项目练习代码
'''
# @version :Version1.0
# Modification description:
'''
'''
# 表格打印
# 编写一个名为printTable()的函数,它接受字符串的列表的列表,将它显示在组织良好的表格中,
# 每列右对齐。假定所有内层列表都包含同样数目的字符串,如:
tableData = [
['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']
]
# 输出格式应为:
'''
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
'''
# 程序
def printTable(listdata):
colWidths = [0] * len(listdata)
for i in range(len(listdata)):
for j in range(len(listdata[i])):
if colWidths[i] < len(listdata[i][j]):
colWidths[i] = len(listdata[i][j])
# print(colWidths)
for i in range(len(listdata[i])):
for j in range(len(listdata)):
print(listdata[j][i].rjust(colWidths[j]), end = ' ')
print()
printTable(tableData)
# 注意打印输出列表元素的先后顺序