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
#1
I have a folder lets say as 'input_Folder' which has a list of CSV files with data. I'm trying to write a python code which reads this list of CSV files from the input_folder and creates a master CSV file with two columns.

The columns in the master CSV files are 'Scenario' and 'Status'

Column Name requirement are as follows,
Scenario = Name of the file from the directory and
Status = if the file has a value in the second row of second column then populate as 'Pass' else 'Fail'
Below is my code. After executing the code i'm able to see Master CSV created but with empty lines. I'm quite new to python so could somebody help me out here please

import os
import csv

path = (Input file path)

with open(SUMMARY.csv", 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['SCENARIO', 'STATUS'])
for files in os.walk(path):
for filename in files:
with open(input_file.csv") as csv_file: #checking if the code is working for
#one sample file
all_rows = list(csv_file)
line_count = 0
for row in all_rows[1:2]:
if line_count == 1:
if row[1].value == none:
writer.writerow([os.path.basename(filename).split(".")
[0],'PASS'])
else:
writer.writerow([os.path.basename(filename).split(".")
[0],'FAIL'])
line_count += 1
Larz60+ write Feb-13-2022, 08:33 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

2nd Notice. Please use BBcode tags.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 429 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  Unable to understand the meaning of the line of code. jahuja73 0 313 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Create Choices from .ods file columns cspower 3 615 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,902 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 678 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,474 Nov-09-2023, 10:56 AM
Last Post: mg24
  Create csv file with 4 columns for process mining thomaskissas33 3 760 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,134 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,127 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,300 Jun-19-2023, 02:12 PM
Last Post: DosAtPython

Forum Jump:

User Panel Messages

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