0

Disclaimer: I have not had any training in Python. I have learnt HTML, CSS, C but many years ago and don't remember much.

I am doing a HW for a chemical engineering class. The questions asks to a) create a csv file using python and input the given data. Each row of the csv should contain data of one distribution with the first row of the csv file havng the word "grade" b) read the said csv file and store the data and store this data in an array and c) create an histogram

the code I wrote was:

import csv
with open('data.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Grade'])
    spamwriter.writerow(['40'])
    spamwriter.writerow(['60,60,60'])
    spamwriter.writerow(['80,80,80,80'])
    spamwriter.writerow(['100,100'])


import csv

results = []
with open("data.csv") as csvfile:
    reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) # change contents to floats

    for row in  imdb_data: # each row is a list
        results.append(row)

For this code, it works if the "grade" data row is not there as this is a float. Anyone know how to skip this line of code? I was thinking if there was a command like row+1 or a for loop which can skip it but don't know how to implement it.

  • You may find this helpful https://stackoverflow.com/questions/11349333/how-to-ignore-the-first-line-of-data-when-processing-csv-data/11350095#11350095 – user3500142 Oct 12 '19 at 22:15
  • You want to skip the first row when reading? – wwii Oct 12 '19 at 22:15
  • your CSV writing code is very strange! why write a CSV formatted string to a column in a "space separated" file with strange string quoting? i.e. why aren't you just opening it normally, and doing e.g. `writerow([60, 60, 60])` – Sam Mason Oct 12 '19 at 22:31
  • If you *know* the csv file has a header, you can skip it with `next(reader)`. – martineau Oct 12 '19 at 23:32

0 Answers0