Python Forum
Loop files - Extract List Data To Individual Columns in CSV
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop files - Extract List Data To Individual Columns in CSV
#1
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.

#!/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)
#done
Result should be
Output:
Col 1 - Apple | Col 2 - Pear | Col 3 - Cherry Apple 1 | Pear 1 | Cherry 1 Apple 2 | Pear 2 | Cherry 2 etc
At 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 Confused
I would appreciate that

Thank you



:)


Python newbie trying to learn the ropes
Reply


Messages In This Thread
Loop files - Extract List Data To Individual Columns in CSV - by dj99 - May-18-2019, 12:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 263 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 520 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  Help with to check an Input list data with a data read from an external source sacharyya 3 459 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,110 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 893 Feb-10-2024, 07:08 AM
Last Post: buran
  Why can't it extract the data from .txt well? Melcu54 3 692 Aug-20-2023, 10:07 PM
Last Post: deanhystad
Question Need help for a python script to extract information from a list of files lephunghien 6 1,137 Jun-12-2023, 05:40 PM
Last Post: snippsat
  script to calculate data in csv-files ledgreve 0 1,134 May-19-2023, 07:24 AM
Last Post: ledgreve
  list the files using query in python arjunaram 0 693 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Extracting Data into Columns using pdfplumber arvin 17 5,711 Dec-17-2022, 11:59 AM
Last Post: arvin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020