I have a text file (abc.txt) which contains following:
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
I want to get all the data before "hello" word and save it in a csv file. which means there will be two rows based on the above data :
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
So far I tried attached code in the jpg file (of course it will just give an empty csv file)
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");
Can anyone please help me how to write this code so that I can get all_data.csv file with the above mentioned two rows? The dataset I have shown in text file is just a case study. My original file contains a lot of data but same pattern.
Thanks in advance :)
python code that I wrote so far
P.S. I am new in Python. Just started learning.
https://stackoverflow.com/questions/66837935/extract-data-using-python March 28, 2021 at 10:40AM
没有评论:
发表评论