繁体   English   中英

Python:从文本文件中获取行并搜索以“from”开头的单词并输出这些行

[英]Python: taking lines from a text file and searching words that start with "from" and outputting those lines

我正在尝试 output 一个大的电子邮件文件,并试图提取其中以“from”开头的行。 “初级蟒蛇”

x = open('text_file.txt','w')

y = input('请输入文件名后跟.txt:')

打印(y)

对于 x 中的名称:

 if name.startswith('From'): xname = name[0:] print(xname)

这里基本上是我想要实现的 output

在此处输入图像描述

请试试这个:

with open('_my_file.txt', 'r') as _my_file:
    # Iterates over the lines in file
    for _ in _my_file.readlines():
        # Ignore the case while searching
        if _.casefold().startswith('from'):
            print(_)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM