0

I made a function that reads a csv file and processed some information so I can show it in a plot in R. But with the function that I have right now my processed csv file look something like this:

1   2   3   4   12   15   16

But what I want is this in my csv file:

1
2
3
4
12
15
16

Here is what I have so far:

import csv
from datetime import datetime
from unittest import skip
end_date = datetime(day=5, month=10, year=2020, hour=23, minute=59)
fastStudent = []
allPassedStudents = []

# Open the csv file with all of the values that need to be proccesed
with open('OOP_eindopdracht\\fed_results\\WFHBOICT.OOP.20.EPIC.EXPORT.FED.20.csv', 'r') as read_obj:
    # reads the whole table with as seperator ";"
    csv_dict_reader = csv.reader(read_obj, delimiter=';',)
    # Skips the headers
    next(csv_dict_reader)
    for row in csv_dict_reader:
        
        #Looks if us11 is empty or not, if its not empty the student has passed
        if row[11]:
            # puts the students(row[0]) in allpassedStudents so it can be saved in a csv file
            allPassedStudents.append(row[0])
            # Converts the date thats in the csv(row[11]) in to datetime so it can be compared to end_date
            passedDate = datetime.strptime(row[11], '%d-%m-%Y %H:%M') 
    
        if passedDate:
            # look if passedDate is before the end_date and if so add it to fastStudent so it can be saved in a csv file
            if passedDate < end_date:
                fastStudent.append(row[0])

    with open("OOP_eindopdracht\\Eindopdracht_3\\Processed_csv_file\\Behaalde_studenten.csv", 'a') as f_object:  
        # Pass the CSV  file object to the writer() function
        writer_object = csv.writer(f_object)
        # Result - a writer object
        # Pass the data in the list as an argument into the writerow() function
        writer_object.writerow(allPassedStudents)  
        # Close the file object

    with open("OOP_eindopdracht\\Eindopdracht_3\\Processed_csv_file\\Snelle_studenten.csv", 'a') as f_object:  
        # Pass the CSV  file object to the writer() function
        writer_object = csv.writer(f_object)
        # Result - a writer object
        # Pass the data in the list as an argument into the writerow() function
        writer_object.writerow(fastStudent)  
        # Close the file object
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
jenak
  • 1
  • What does `allPassedStudents` contain before you write it out? Is it a list of numbers or is it a list of lists which contain numbers? If the later, then you just need to iterate over the rows and write them each to the CSV. – Code-Apprentice Feb 15 '22 at 15:08
  • @Code-Apprentice something like this: ['1', '2', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14', '15', '16', '17', '19', '20', '21', '22', '23', '25', '26', '27', '28', '29', '30', '31', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '47', '49', '50', '51', '52', '53', '54', '55', '56', '57', '59', '60', '61', '62', '63', '64', '65', '66', '68', '71', '73', '74', '75', '76', '77', '78', '79', '81', '83', '85', '86', '87', '88', '89','] – jenak Feb 15 '22 at 15:10
  • 1
    You are passing the entire list to `writerow`. You should be iterating and pasting one element per row. See the accepted answer on the duplicate question – not_speshal Feb 15 '22 at 15:10
  • @jenak So first step, you need to rethink your data structure. That looks like a single row, which is why you get the output you do. Instead, you need to make something to hold multiple rows. – Code-Apprentice Feb 15 '22 at 15:11
  • @not_speshal the accepted answer worked but if I look at my csv file now the numbers 10 is seperated in 2 field so 1 is in field A1 and 0 in field A2. Like this: |1|0|. how could I make it so 10 is just in one field ? – jenak Feb 15 '22 at 15:19
  • @jenak I suggest that you debug your code by adding `print()` statements to see what is going on. If you still need help, you will have to show the exact code that causes that output. It is probably best to post a new question. – Code-Apprentice Feb 15 '22 at 16:47

0 Answers0