I'm trying to print the contents of a CSV file row by row, there's only one row of data (CSV Library adds an extra row at the end of the file for some reason) however when the contents are printed below the contents of the file is outputted twice. From my understanding this is due to the extra blank line inputted by the CSV Library. I'm looking for a way to ignore this blank line or get rid of it entirely.
hourList = []
with open("reports/totalHours.csv", "r") as report:
reader = csv.reader(report)
for row in reader:
hourList.append(row)
print(hourList)
the contents of totalHours.csv:
000,40
cmd output of script
[['000', '40']]
[['000', '40'], []]
PS. I'm writing to the file initially using Append as this is the best method that achieves my required output.
CodePudding user response:
This implementation will ignore the blank line at the bottom of the file, but it will also ignore any other blank lines in the file:
hourList = []
with open("reports/totalHours.csv", "r") as report:
reader = csv.reader(report)
for row in reader:
if len(row) > 0:
hourList.append(row)
print(hourList)