【发布时间】:2021-07-22 08:18:43
【问题描述】:
我正在尝试从我的计算机目录中的多个文本文件中解析一些数据。
我想将每个文件中的特定信息复制到单独的文本文件中。我犯了一些错误,但不明白是什么。请看下面的代码。非常感谢您的帮助。
texts = []
for file in OK_files:
with open(path + '\\' + file) as f:
# this t varialble store your 2nd line in each cycle
t = f.read().split('\n') [1]
# this file append each file into texts list
texts.append(t)
# to print each important line
with open("C:/Users/rafi.nazmul/Documents/Output","w+") as file:
for text in texts:
file.write(texts)
我遇到了错误:
TypeError: write() argument must be str, not list
更新
错误已从 cmets 修复,但我需要了解更多信息:
- 代码现在只从每个文件中读取一行,第一个索引。我怎样才能 从每个文件中读取多行?
- 如何从每一行而不是整行中选择特定字符?
【问题讨论】:
-
请在此处粘贴代码
-
你的意思是整个代码?但是这里超过了字数限制!
标签: python list parsing text-files writing