【发布时间】:2021-12-07 05:36:57
【问题描述】:
我在一个文件夹中有多个文本文件,其中数据的形式是- text file 1 和 text file 2
我想从文本文件 1 中提取文件名和 IOS 值,从文本文件 2 中提取文件名和 MB/s 值并将其存储在 excel 文件中
这是我的代码-
import os
path = "Folder Path"
os.chdir(path)
def read_text_file(file_path):
with open(file_path, 'r') as f:
print(f.read())
for file in os.listdir():
if file.endswith(".txt"):
file_path = f"{path}\{file}"
read_text_file(file_path)
with open ('text file 1.txt') as f:
for line in f:
read = line.spilt(" ")
print (IOS)
with open ('text file 2.txt') as f:
for line in f:
read = line.spilt(" ")
print (BW)MB/s
我该如何解决这个问题?
【问题讨论】:
-
您提供的代码包含许多语法错误。你需要修复这些。然后你需要决定文件格式规则是什么。文件名是否与文件中的第一行相同(减去冒号)? IOS令牌总是在最后一行吗?一旦您认为通过它应该非常简单
-
@BrutusForcus 是的,文件名与文件中的第一行相同,IOS 和 MB/s 不是文本文档的最后一行,为方便起见,我裁剪了其余数据跨度>