Hi friends ,
I am trying to extract some data to a CSV file.
I have an list of values - that should be extracted from each text file.
If there are 3 array values - there should be 3 columns in the CSV.
I wasn’t able to work out the correct way to output the results to my csv file.
Please may some one have a look at this, I have been around in circles trying to work it out
I would appreciate that
Thank you
I am trying to extract some data to a CSV file.
I have an list of values - that should be extracted from each text file.
If there are 3 array values - there should be 3 columns in the CSV.
I wasn’t able to work out the correct way to output the results to my csv file.
#!/usr/bin/python3
import os
import csv
import pandas
def extract_lines_from_files(filename, dirpath):
filename = os.path.join(dirpath, filename)
search_keywords = ['Apple','Pear','Cherry'] # 3 Columns
with open(filename, 'r',errors='ignore') as Text_file:
for line in Text_file:
found = False
for word in search_keywords:
if word in line:
found = True
if found:
lines = []
lines.append(line)
with open("a.csv",'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([line])
#main
directory = 'C:/Users/home/Desktop/files/'
for root, dirs, files in os.walk(directory):
for f in files:
extract_lines_from_files(f, directory)
#doneResult should beOutput: Col 1 - Apple | Col 2 - Pear | Col 3 - Cherry
Apple 1 | Pear 1 | Cherry 1
Apple 2 | Pear 2 | Cherry 2
etcAt the moment its all output to 1 column.Please may some one have a look at this, I have been around in circles trying to work it out
I would appreciate that
Thank you
:)
Python newbie trying to learn the ropes
