python - 如何在 python 中解析文本文件并打印到另一个文本文件或 json 文件中?
问题描述
我有一个文本文件,它基本上是一个列表,如:
CustomId: 123412412
Name: blah blah
我想逐行解析这个文本文件,并且只打印我最终会指定的我需要的行。现在我可以解析文件并打印它。但是,我在尝试思考将解析并仅打印列表中所需行的逻辑时遇到了麻烦。
解决方案
def find(search, file_input_path, file_output_path):
output = open(file_output,"a")
f = open(file_input_path,'r')
lines = f.readlines()
for l in lines:
if(l == search):
output.write(s[1]))
output.close()
f.close()
使用示例:
find("Name: blah blah", "input.txt", "output.txt")
将在另一个文件中产生如下输出:
Name: blah blah
如果您想查找更多行,只需在循环中调用该函数。
推荐阅读
- mysql - 将7天添加到时间戳日期mysql
- javascript - 画布javascript动画-从左到右运行的矩形改变颜色-动画重复问题
- spring - 使用 Apache Camel 在 Spring Boot 中创建消息队列
- java - 使用 spring java 的千分尺度量(没有 spring boot)
- css - NgTemplate Bootstrap 不接受我的 CSS 类
- javascript - 重构 JavaScript/TypeScript:消除重复的代码块
- java - 绝对 uri: [http://www.springframework.org/tags/form] 无法在 web.xml 或随此应用程序部署的 jar 文件中解析
- angular - 如何在 NgRx-Effects 中测试多个 withLatestFrom 商店注入?
- c# - 如何在.net core web api服务器上处理从xamarin发送的请求
- c# - 如何在 HttpClient 中向 Google Oauth2 SSO 发送 Rest 请求时修复“错误请求”错误