Python Forum
Python code to read second line from CSV files and create a master CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code to read second line from CSV files and create a master CSV file
#2
One way

#! /usr/bin/env python3
import csv
import os
from tabulate import tabulate

with open('summary.csv', 'w') as file:
    writer = csv.writer(file)
    writer.writerow(['SCENARIO', 'STATUS'])

files = os.listdir('input_folder')
for file in files:
    with open(f'input_folder/{file}', 'r') as csv_file:
        read_file = csv.reader(csv_file)

        for line in read_file:
            with open('summary.csv', 'a') as summary:
                if line[1] == ' ':
                    status = 'FAIL'
                else:
                    status = 'PASS'

                writer = csv.writer(summary)
                writer.writerow([f'input_folder/{file}', status])

with open('summary.csv', 'r') as file:
    read_file = csv.reader(file)
    print(tabulate(read_file, headers='firstrow'))
Output:
SCENARIO STATUS ----------------------- -------- input_folder/file_1.csv PASS input_folder/file_1.csv PASS input_folder/file_1.csv PASS input_folder/file_2.csv PASS input_folder/file_2.csv FAIL input_folder/file_2.csv PASS
BashBedlam and Pedroski55 like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
RE: Python code to read second line from CSV files and create a master CSV file - by menator01 - Feb-13-2022, 07:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 239 May-03-2024, 07:23 AM
Last Post: Pedroski55
  How does this code create a class? Pedroski55 6 528 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  Unable to understand the meaning of the line of code. jahuja73 0 329 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Create Choices from .ods file columns cspower 3 652 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,952 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 722 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,560 Nov-09-2023, 10:56 AM
Last Post: mg24
  Create csv file with 4 columns for process mining thomaskissas33 3 802 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,160 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,160 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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