【发布时间】:2021-06-24 12:31:16
【问题描述】:
我有一个文本文件 (abc.txt),其中包含以下内容:
10 20 0 #1st line
-9 -9 -9 -9 #2nd line
4 4 #3rd line
2 2 0. 0. hello #4th line
15 25 1 #5th line
-9 -9 0 1 #6th line
5 5 #7th line
7 7 8. 8. hello #8th line
我想获取 "hello" 单词之前的所有数据并将其保存在 csv 文件中。这意味着根据上述数据将有两行:
Ist row: 10 20 0 -9 -9 -9 -9 4 4 1 1 2 2
2nd row: 15 25 1 -9 -9 0 1 5 5 7 7 8 8
到目前为止,我尝试了 jpg 文件中的附加代码(当然它只会给出一个空的 csv 文件)
with open(inputDir + "all_data.csv", "w") as output1File:
output = "";
inputFile = inputDir + fileName;
with open(inputFile, "r") as inputFile:
for line in inputFile:
if line.endswith(" name"):
values = line.split(" ")
value = values[1].strip();
output = output + value + "\n" ;
output1File.write(output + "\n");
谁能帮助我如何编写这段代码,以便我可以得到上面提到的两行的 all_data.csv 文件?我在文本文件中显示的数据集只是一个案例研究。我的原始文件包含大量数据但模式相同。
提前致谢:)
python code that I wrote so far
附:我是 Python 新手。刚开始学习。
【问题讨论】:
-
谢谢。抱歉,这是我在 stackoverflow 中的第一篇文章。我又试了一次,看起来代码现在是 TEXT 了。