Python编程快速上手-让繁琐工作自动化 — 读书与代码笔记

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)

# 注意打印输出列表元素的先后顺序

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值