社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python:如何从txt文件中的特定单词开始读取到文件的末尾

Appries • 4 年前 • 840 次点击  

我想从一个特定的单词到文件的末尾读取txt文件。

例子:

如果它们在.txt文件中

A部分:

B部分
B部分内容

C部分内容

在这里,我想从c部分或从c部分开始提取或只读

我知道从中间抽出

with open(file1) as file:
    text = file.read().lower().split("part b")[1].split("part c")[0].strip()

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55415
 
840 次点击  
文章 [ 2 ]  |  最新文章 4 年前
lunesco
Reply   •   1 楼
lunesco    5 年前

也许很简单:

with open(file1) as file:
    text = file.read().lower().split("part c")[1]
Reedinationer
Reply   •   2 楼
Reedinationer    5 年前

你很接近。只需在c部分分割它,然后从结果列表中取出第二项。

with open(file1) as file:
    text = " ".join(file.read().lower().split("part c")[1:]).strip()