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
#4
#!/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']                              
 
    with open(filename, 'r',errors='ignore') as f:
        lines = []
        
        # the file object must be read first
        for line in f.readlines():

            # (word in line) expression returns either True or False
            if any((word in line) for word in search_keywords):

                # strip() removes "\n" (new line byte)
                lines.append(line.strip())
              
        with open("a.csv",'a', newline='') as csv_file:

            # delimeter is what separates the values in the csv file
            # btw csv = "comma separated values"
            writer = csv.writer(csv_file, delimiter='|')
            writer.writerow(lines)
            
directory = 'C:/Users/home/Desktop/files/'
for root, dirs, files in os.walk(directory):

    # using only the files that end with .txt
    # this way the files could be in the same directory with the .py file
    for f in filter(lambda x: x.endswith('.txt'), files):
        extract_lines_from_files(f, directory)
Contents of the "a.csv" file:
Output:
Apple 1|Pear 1|Cherry 1 Apple 2|Pear 2|Cherry 2
Reply


Messages In This Thread
RE: Loop files - Extract List Data To Individual Columns in CSV - by michalmonday - May-18-2019, 02:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 362 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 721 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 550 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,222 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 1,020 Feb-10-2024, 07:08 AM
Last Post: buran
  Why can't it extract the data from .txt well? Melcu54 3 737 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,200 Jun-12-2023, 05:40 PM
Last Post: snippsat
  script to calculate data in csv-files ledgreve 0 1,177 May-19-2023, 07:24 AM
Last Post: ledgreve
  list the files using query in python arjunaram 0 717 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Extracting Data into Columns using pdfplumber arvin 17 5,952 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