def read_file(file):
with open(file, 'r', encoding='utf-8') as f:
return f.read() # 返回值为字符串
str_file = read_file('mayun.txt')
#第一步输出
def count_str(data):
up_count = 0
low_count = 0
num = 0
blank_num = 0
other_num = 0
for i in data:
if i.isupper():
up_count+=1
elif i.islower():
low_count+=1
elif i.isdigit():
num += 1
elif i.isspace():
blank_num += 1
else:
other_num += 1
return up_count,low_count,num,blank_num,other_num
up_count,low_count,num,blank_num,other_num = count_str(str_file)
print(up_count,low_count,num,blank_num,other_num)
#第二步输出
import string
m = str_file
for i in string.punctuation:
m = m.replace(i,' ')
s = m
my_list = s.split(' ')
count = len(my_list)
print(count)
```python
#第二步输出我的程序是96 答案是84
不知道哪里出问题了 求指点
看了你的代码,有几个可能的问题:
1. 你的第二步输出计算的是词数,如果文本中有很多重复词,会导致词数过高。答案84可能是计算的不重复词数。你可以在split以后,再用set去重,然后取len。
2. 你的replace可能没替换干净,像"..."这样的可能被你遗漏了,导致词数算高。你可以把string.punctuation扩展到更全面,或者直接用re.sub()替换所有非字母数字的字符。
3. 你的文本中可能含有很多空格,tab等空白符,这也会导致split得出的词数过高。你可以先用strip()去除空白符,然后再split。
总之,我的建议是:
1. 用strip()先去除空白符
2. 用re.sub()替换所有非字母数字的字符为空格
3. split得到列表
4. 用set去重
5. 取len
修改后的代码如下:
python
import re
import string
m = str_file.strip()
m = re.sub(r'[^A-Za-z0-9]', ' ', m)
my_list = m.split(' ')
my_set = set(my_list)
count = len(my_set)
print(count)
希望这个能帮助你解决问题,让你的结果更接近标准答案。
库名称简介Mimetypes,Python标准库,映射文件名到MIME类型。
imghdr→ Python标准库,确定图像类型。
python-magic→ libmagic文件类型识别库,Python接口格式。
path.py→ os.path模块的二次封装。
watchdog→ 一组API和shell实用程序,用于监视文件系统事件。
Unipath→ 面向对象的文件/目录的操作工具包。
pathlib→ (Python 3.4版已经作为Python标准库),一个跨平台,面向path的函数库。pickle/cPickle,python的pickle模块实现了基本的数据序列和反序列化。通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储;通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象。
cPickle→是[C语言]实现的版本,速度更快。
ConfigParser→ Python标准库,INI文件解析器。
configobj→ INI文件解析器。
config→ 分层次配置,logging作者编写。
profig→ 多格式配置转换工具。
logging→ Python标准库,日志文件生成管理函数库。
logbook→ logging的替换品。
Sentry→ 实时log服务器。
Raven→ 哨兵Sentry的Python客户端。
Sphinx→ 斯芬克斯(狮身人面像),Python文档生成器。
reStructuredText→ 标记语法和解析工具,Docutils组件。
mkdocs→ Markdown格式文档生成器。
pycco→ 简单快速、编程风格的文档生成器。
pdoc→ 自动生成的Python库API文档epydoc,从源码注释中生成各种格式文档的工具
问题解答:
对于文本分析和加密的问题,可以使用Python标准库中的相关模块来实现。具体的解决方案如下:
先将文本文件读入Python程序中,可以使用内置函数open()
打开文件,并使用readlines()
函数逐行读取文件内容。然后,使用正则表达式re
模块来进行关键字的提取。使用re.compile()
函数进行正则表达式的编译,然后将编译后的正则表达式作为参数传递给re.findall()
函数,即可获取所有匹配的结果。例如:
import re
# 打开文件并逐行读取内容
with open('text.txt', 'r') as f:
lines = f.readlines()
# 编译正则表达式
pattern = re.compile(r'\b\w+\b')
# 提取关键字
keywords = []
for line in lines:
matches = pattern.findall(line)
keywords += matches
# 输出结果
print(keywords)
Python中内置的base64
模块提供了Base64加密的功能。使用该模块的b64encode()
函数来加密字符串,使用b64decode()
函数来解密字符串,例如:
import base64
# 加密明文
plaintext = 'Hello, world!'
ciphertext = base64.b64encode(plaintext.encode('utf-8'))
# 输出加密结果
print(ciphertext) # b'SGVsbG8sIHdvcmxkIQ=='
# 解密密文
plaintext = base64.b64decode(ciphertext).decode('utf-8')
# 输出解密结果
print(plaintext) # Hello, world!
为了保证加密文本的安全性,可以使用对称加密算法和非对称加密算法来进行加密,然后再使用数字签名算法来验证文本的完整性。对称加密算法指的是加密和解密使用同一个密钥的算法,比如常用的AES算法,而非对称加密算法指的是加密和解密使用不同密钥的算法,比如常用的RSA算法。数字签名算法使用了Hash算法和非对称加密算法的特性,可以对文本进行数字签名,以保证文本的完整性和安全性。Python中常用的加密算法和数字签名算法都可以使用pycrypto
库来实现。
代码示例:
# 安装pycrypto库
!pip install pycrypto
# 导入相关模块
from Crypto.Cipher import AES, PKCS1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256, SHA512
import base64
# 对称加密算法
def aes_encrypt(key, text):
length = 16
pad = lambda s: s + (length - len(s) % length) * chr(length - len(s) % length)
raw = pad(text)
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
encrypted = cipher.encrypt(raw.encode('utf-8'))
return base64.b64encode(encrypted).decode('utf-8')
# 对称解密算法
def aes_decrypt(key, text):
unpad = lambda s: s[0:-ord(s[-1])]
encrypted = base64.b64decode(text)
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
decrypted = cipher.decrypt(encrypted)
return unpad(decrypted.decode('utf-8'))
# 非对称加密算法
def rsa_encrypt(pub_key, text):
key = RSA.importKey(pub_key.encode('utf-8'))
cipher = PKCS1_v1_5.new(key)
encrypted = cipher.encrypt(text.encode('utf-8'))
return base64.b64encode(encrypted).decode('utf-8')
def rsa_decrypt(priv_key, text):
key = RSA.importKey(priv_key.encode('utf-8'))
cipher = PKCS1_v1_5.new(key)
decrypted = cipher.decrypt(base64.b64decode(text), 'ERROR')
return decrypted
# 数字签名
def sign(data, priv_key):
key = RSA.importKey(priv_key)
h = SHA512.new(data)
signer = Signature_PKCS1_v1_5.new(key)
signature = signer.sign(h)
return signature
def verify(data, signature, pub_key):
key = RSA.importKey(pub_key)
h = SHA512.new(data)
verifier = Signature_PKCS1_v1_5.new(key)
return verifier.verify(h, signature)
以上就是针对文本分析和加密的解决方法及代码示例。