-3

I took a course to learn python; In my course, I have to do a project with the following conditions:

  • I have to use a template which is prepared by the teacher.
  • All the codes should be written in function
  • It needs to open a CSV file and have a file (txt/csv) as the output.

Now, my question is how to refer to the files with the following structure:

def function (input_file_name, output_file_name):

Below please find a sample and simple code:

import csv

def function(input_file):

    with open ('filename.csv') as input_file:

        reader = csv.reader(input_file_name)

        for row in reader:

            print(row)

but there is no output in my terminal!

What is my mistake?

Hasan A.
  • 30
  • 7

3 Answers3

1

Your approach might be something like this:

import csv


def somefunction(in_filepath, out_filepath):
    with open(in_filepath) as infile:
        with open(out_filepath, 'w') as outfile:
            reader = csv.reader(infile)
            writer = csv.writer(outfile)

            for row in reader:
                print('Do something with row')
                writer.writerow(row)

Add whatever processing you want, in the for loop. The function should be called with str or Pathlike objects naming the input and output files. E.g.

somefunction("in.csv", "out.csv")
L.Grozinger
  • 1,931
  • 1
  • 8
  • 19
0

You can use builtin open() function for this, like open(input_file) and then read all lines as list with .readlines(), read each line with .readline() with while loop and all as string with .read()

Wasif
  • 13,656
  • 3
  • 11
  • 30
  • I can write a code without those restrictions which I mentioned above and it has a correct output; However, I do not know how to write a code to open a file, where the file is an input of the function – Hasan A. Nov 16 '20 at 19:14
0

Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Python’s built-in open() function, which returns a file object. This is then passed to the reader, which does the heavy lifting.

import csv

with open('employee_birthday.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
    if line_count == 0:
        print(f'Column names are {", ".join(row)}')
        line_count += 1
    else:
        print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
        line_count += 1
print(f'Processed {line_count} lines.')

You can also write to a CSV file using a writer object and the .write_row() method:

import csv

with open('employee_file.csv', mode='w') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

employee_writer.writerow(['John Smith', 'Accounting', 'November'])
employee_writer.writerow(['Erica Meyers', 'IT', 'March'])
  • Thanks for your answer, but I can not add any code outside the functions, the difficult part is that. I do not know how to define a function which the respective file is its input – Hasan A. Nov 16 '20 at 18:31