python - 如果该行中的字符串与python匹配,如何打印变量内的整行文本?
问题描述
我有一个带有一些文本行的变量,我只想用 python 打印具有该特定字符串的行,如果我是 grep 命令但使用 python,而不是,我不想保存文本在一个文件中,然后解析它并打印该行,我想在内存中执行此操作,你知道,只使用变量。
解决方案
# the string you want so search from
some_string = 'first line /n second line'
# split the string into a list divided by '/n' (enter)
some_string = some_string.split('/n')
# print the lines that have a specific word
word = 'second'
for line in some_string:
if word in line:
print(line)
一个例子是这样的。您可以随意修改它,以便更好地适应您的问题
推荐阅读
- android - Firebase 作为我正在创建的 Flutter 手机游戏的服务器会是一个不错的选择吗?
- sql-server - 循环或多个级联路径 SQL Server
- powershell - 获取计算机上的最后登录用户
- tensorflow - 我可以自定义预训练的 Transformer 神经机器翻译模型的字典吗?
- python - 为什么以前使用过我的歌曲变量不等于输入
- r - 如何使用单个字符串引用数据框列
- java - 方法不是按时间顺序调用的
- amazon-web-services - 如何使用 aws CLI 过滤以匹配在一系列日期中开始的 ec2s 以获取包含像 StateTransitionReason 这样的日期的字符串值?
- python - 如何根据条件聚合功能?
- jquery - Divs 的数字适合浏览器窗口并调整大小