Python编程快速上手实践项目-多重剪贴板,疯狂题词和正则查找

Python编程快速上手实践项目-多重剪贴板,疯狂题词和正则查找

首页休闲益智疯狂填字更新时间:2024-05-11

多重剪贴板

扩展本章中的多重剪贴板程序,增加一个 delete <keyword>命令行参数,它将 从 shelf 中删除一个关键字。然后添加一个 delete 命令行参数,它将删除所有关键字。

import shelve import pyperclip from sys import argv mcbshelf = shelve.open('mcbb') if len(argv) == 3 and argv[1].lower() == 'save': mcbshelf[argv[2]] = pyperclip.paste() elif len(argv) == 3 and argv[1].lower() == 'delete': del mcbshelf[argv[2]] print('delete {}'.format(argv[2])) elif len(argv) == 2: if argv[1].lower() == 'list': pyperclip.copy(str(list(mcbshelf.keys()))) elif argv[1].lower() == 'delete': mcbshelf.clear() print('clear.') elif argv[1] in mcbshelf: pyperclip.copy(mcbshelf[argv[1]]) mcbshelf.close()

疯狂填词

创建一个疯狂填词(Mad Libs)程序,它将读入文本文件,并让用户在该文本文件中出现 ADJECTIVE、NOUN、ADVERB 或 VERB等单词的地方,加上他们自己的文本。例如,一个文本文件可能看起来像这样:

The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.

程序将找到这些出现的单词,并提示用户取代它们。

Enter an adjective:

silly

Enter a noun:

chandelier

Enter a verb:

screamed

Enter a noun:

pickup truck

以下的文本文件将被创建:

The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events.

结果应该打印到屏幕上,并保存为一个新的文本文件。

Bereplace = ['ADJECTIVE', 'NOUN', 'ADVERB', 'VERB' ] with open('fktc.txt') as f: cont = f.read() for i in range(len(Bereplace)): text = input('Enter and {}: '.format(Bereplace[i])) cont = cont.replace(Bereplace[i],text) print(cont) with open('new_fktc.txt', 'w') as f: f.write(cont)

Enter and ADJECTIVE: aaaaaaaa Enter and NOUN: bbbbbbbbbbbb Enter and ADVERB: ccccccccccc Enter and VERB: dddddddddddddd The aaaaaaaa panda walked to the bbbbbbbbbbbb and then dddddddddddddd. A nearby bbbbbbbbbbbb was unaffected by these events.

正则表达式查找

编写一个程序,打开文件夹中所有的以d开头的txt 文件,查找匹配用户提供的正则表达式的所有行。结果应该打印到屏幕上。

这次我们匹配大写字母

import re,os #将当前目录中的以'd'开头的文件名并且文件内容匹配到大写的字符所在的行打印出来 upperRegex = re.compile(r'([A-Z] )') fileRegex = re.compile(r'd. \.txt') path = '.' for file in os.listdir(path): res = fileRegex.search(file) if res: print(res.group(0),':') filelines = open(file, 'r', encoding='utf-8').readlines() for line in filelines: if upperRegex.findall(line): print('匹配到大写字母的行:' line.strip('\n'))

dist1.txt源文件内容:

The ADJECTIVE panda walked to the NOUN and then VERB. what is your name? A nearby NOUN was unaffected by these events.

dist2.txt源文件内容:

nginx NEWS nginx-1.17.5 mainline version has been released. njs-0.3.6 VERSION has been released... unit-1.12.0 version has been released, featuring several important bugfixes. nginx-1.17.4 mainline version has BEEN released.

dist3.txt源文件内容:

Active kernel releases there are several main categories into ... Prepatch or "RC" kernels are mainline kernel ... they must be compiled from source and usually ... Prepatch kernels are maintained and released mainline tree is maintained by Linus Torvalds. it's the tree where all new features are introduced... NEW mainline kernels are released every 2-3 months.

将匹配到大写字母的所在行打印出来

dist1.txt : 匹配到大写字母的行:The ADJECTIVE panda 匹配到大写字母的行:walked to the NOUN and then VERB. 匹配到大写字母的行:A nearby NOUN was unaffected by these events. dist2.txt : 匹配到大写字母的行:nginx NEWS 匹配到大写字母的行:njs-0.3.6 VERSION has been released... 匹配到大写字母的行:nginx-1.17.4 mainline version has BEEN released. dist3.txt : 匹配到大写字母的行:Active kernel releases 匹配到大写字母的行:Prepatch or "RC" kernels are mainline kernel ... 匹配到大写字母的行:Prepatch kernels are maintained and released 匹配到大写字母的行:mainline tree is maintained by Linus Torvalds. 匹配到大写字母的行:NEW mainline kernels are released every 2-3 months.

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved