python读取pdf txt 文件

pdf

  • 首先安装pdfminer3k pip install pdfminer3k

    • 提示这样就算安装完了
       Successfully installed atomicwrites-1.1.5 attrs-18.1.0 more-itertools-4.2.0 pdfminer3k-1.3.1 pluggy-0.6.0 ply-3.11 py-1.5.4 pytest-3.6.2
      
    • github地址下载可以看文档
  • 开始正式的编码

    • 准备pdf素材 我这里是准备了 笨办法学python的pdf
    • 查看已从github下载好的文档实例

代码如下 ------- 基本都是复制文档的 上面还有很清楚的解析pdf到输出的方案图

from pdfminer.pdfparser import PDFParser, PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
pf = open("D:/project/python/6-25/bbf_learn_python.pdf",'rb')

#创建与文档关联的解释器
parser = PDFParser(pf)

#pdf文档对象
doc = PDFDocument()

#链接解释器和文档对象
parser.set_document(doc)
doc.set_parser(parser)

# 文档初始化
doc.initialize("")

# pdf 资源管理器
resource = PDFResourceManager()

# 参数分析器
laparams = LAParams()


#pdf聚合器
device = PDFPageAggregator(resource, laparams=laparams)
#pdf解释器
interpreter = PDFPageInterpreter(resource, device)


# 使用文档对象得到页面的集合
for page in doc.get_pages():
    # 使用解析器来读取页面
    interpreter.process_page(page)
    # 聚合器获得布局内容
    layout = device.get_result()

    for out in layout:
        print(out.get_text())

读取内容如下 但是后续报错了 我找了很久没找到原因 'LTLine' object has no attribute 可能是因为翻页了 so 肯定是这样

笨办法学 Python (第三版)

欢迎阅读《笨办法学 Python》第三版。本书中译本发布于 https://learn-python-the-hard-way-zh_cn-
translation.readthedocs.org
英文原版地址为 http://learnpythonthehardway.org/book/
Contents:

 (string)

 4:   变量  (variable)

• 前言:笨办法更简单
• 习题
 0:   准备工作
• 习题
 1:   第一个程序
 2:   注释和井号
• 习题
• 习题
 3:   数字和数学计算
• 习题
 和命名
• 习题
 5:   更多的变量和打印
• 习题
 6:   字符串
 和文本
• 习题
 7:   更多打印
• 习题
 8:   打印,打印
 9:   打印,打印,打印
• 习题
• 习题
 10:   那是什么?
• 习题
 11:   提问
• 习题
 12:   提示别人
• 习题
 13:   参数、解包、变量
• 习题
 14:   提示和传递
• 习题
 15:   读取文件
 16:   读写文件
• 习题
• 习题
 17:   更多文件操作
• 习题
 18:   命名、变量、代码、函数
• 习题
 19:   函数和变量
• 习题
 20:   函数和文件
• 习题
 21:   函数可以返回东西
• 习题
 22:   到现在你学到了哪些东西?
 23:   读代码
• 习题
• 习题
 24:   更多练习
• 习题
 25:   更多更多的练习
• 习题
 26:   恭喜你,现在可以考试了!
• 习题
 27:   记住逻辑关系
• 习题
 28:   布尔表达式练习
• 习题
 和   If
• 习题
• 习题
 31:   作出决定
• 习题
 32:   循环和列表
• 习题
• 习题
 34:   访问列表的元素
• 习题
 35:   分支和函数
 36:   设计和调试
• 习题
• 习题
 37:   复习各种符号

 29:   如果  (if)
 30: Else

 33: While

对比pdf

图片截图

仔细看是有问题的


数据库

  • 首先安装数据库 pip install pymysql

数据库基本操作

这里就不放数据库安装和基本查询语句,可以百度
贴个简单代码验证数据库是否连接成功

import pymysql as pmy
# 连接数据库
connect = pmy.connect('localhost','root','','test_pachong')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = connect.cursor()
# 使用 execute()  方法执行 SQL 查询 
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取数据.
data = cursor.fetchmany()
# data = cursor.fetchmany(size=number)

print("DATABASE VERSION:%s"% data)
# 关闭数据库连接
connect.close()

TXT

log文件其实和txt一样 这里我就用log做个处理,具体数据太多 先贴少部分 一共9万多行

Jun 24 03:32:02 localhost sshd[22201]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jun 24 03:32:04 localhost sshd[22201]: Failed password for root from 182.100.67.120 port 61774 ssh2
Jun 24 03:32:04 localhost sshd[22201]: error: maximum authentication attempts exceeded for root from 182.100.67.120 port 61774 ssh2 [preauth]
Jun 24 03:32:04 localhost sshd[22201]: Disconnecting: Too many authentication failures [preauth]
Jun 24 03:32:04 localhost sshd[22201]: PAM 5 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=182.100.67.120  user=root
Jun 24 03:32:04 localhost sshd[22201]: PAM service(sshd) ignoring max retries; 6 > 3
Jun 24 03:32:05 localhost sshd[26484]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=182.100.67.120  user=root
Jun 24 03:32:05 localhost sshd[26484]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jun 24 03:32:07 localhost sshd[26484]: Failed password for root from 182.100.67.120 port 34196 ssh2
Jun 24 03:32:08 localhost sshd[26484]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jun 24 03:32:11 localhost sshd[26484]: Failed password for root from 182.100.67.120 port 34196 ssh2
Jun 24 03:32:11 localhost sshd[26484]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jun 24 03:32:13 localhost sshd[26484]: Failed password for root from 182.100.67.120 port 34196 ssh2
Jun 24 03:32:14 localhost sshd[26484]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jun 24 03:32:16 localhost sshd[26484]: Failed password for root from 182.100.67.120 port 34196 ssh2
Jun 24 03:32:16 localhost sshd[26484]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jun 24 03:32:18 localhost sshd[26484]: Failed password for root from 182.100.67.120 port 34196 ssh2
Jun 24 03:32:19 localhost sshd[26484]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jun 24 03:32:21 localhost sshd[26484]: Failed password for root from 182.100.67.120 port 34196 ssh2
Jun 24 03:32:21 localhost sshd[26484]: error: maximum authentication attempts exceeded for root from 182.100.67.120 port 34196 ssh2 [preauth]
Jun 24 03:32:21 localhost sshd[26484]: Disconnecting: Too many authentication failures [preauth]
Jun 24 03:32:21 localhost sshd[26484]: PAM 5 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=182.100.67.120  user=root
  • 任务(需求)如下
    把访问服务器失败次数超过4次的做个统计 并显示次数(看似很简单,但我花了很久 因为正则不怎么熟悉)
  • 需要 导入re 就是正则的包import re

结果如下 只贴一部分结果不然太多了

IP:112.85.42.53进入服务器2448次
IP:121.18.238.86进入服务器198次
IP:46.17.42.29进入服务器45次
IP:112.85.42.231进入服务器2454次
IP:217.23.14.245进入服务器86次
IP:125.39.58.58进入服务器20次
IP:58.221.72.141进入服务器113次
IP:193.201.224.208进入服务器134次
IP:58.42.228.170进入服务器6次
IP:94.73.247.125进入服务器20次
IP:115.248.207.78进入服务器6次
IP:165.227.37.14进入服务器8次
IP:115.71.1.45进入服务器11次
IP:217.61.96.4进入服务器37次
IP:118.85.194.26进入服务器12次
IP:46.17.42.33进入服务器45次
IP:112.85.42.197进入服务器1106次
IP:137.74.237.192进入服务器6次
IP:106.1.196.147进入服务器6次
IP:191.29.195.254进入服务器29次
IP:37.138.215.17进入服务器30次
IP:167.114.13.150进入服务器10次
IP:115.159.40.28进入服务器61次
IP:185.8.49.228进入服务器15次
IP:179.243.205.200进入服务器29次
IP:193.201.224.218进入服务器132次
IP:209.97.144.136进入服务器8次
IP:112.85.42.201进入服务器534次
IP:151.106.9.33进入服务器20次
IP:91.194.90.211进入服务器10次
IP:36.67.200.85进入服务器85次
IP:117.40.226.56进入服务器33次
IP:182.100.67.201进入服务器2481次
IP:42.7.26.60进入服务器1248次

今天搞得东西还挺多 很舒服 下午接着搞 接着学 加油加油

你可能感兴趣的:(python读取pdf txt 文件)