‘I am trying to write info for 21 days. I am reading from a ‘;’ delimited txt file that contains column headers and 7 days of info. I read info from a file and establish 7 variables (one for each day of the week). I print the variables after creation and they are correct. When I try to use these variables later in the program over a 3 week period (21 days) only the first variable contains data. Any ideas?’
import csv
with open('info.txt') as perf_csv_file:
csv_reader = csv.reader(perf_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:
if line_count == 1:
PerfDay1 = str(row[1])
print(f'tOn {row[0]} ' + PerfDay1)
elif line_count == 2:
PerfDay2 = str(row[2])
print(f'tOn {row[0]} ' + PerfDay2)
elif line_count == 3:
PerfDay3 = str(row[3])
print(f'tOn {row[0]} ' + PerfDay3)
elif line_count == 4:
PerfDay4 = str(row[4])
print(f'tOn {row[0]} ' + PerfDay4)
elif line_count == 5:
PerfDay5 = str(row[5])
print(f'tOn {row[0]} ' + PerfDay5)
elif line_count == 6:
PerfDay6 = str(row[6])
print(f'tOn {row[0]} ' + PerfDay6)
elif line_count == 7:
PerfDay7 = str(row[7])
print(f'tOn {row[0]} ' + PerfDay7)
‘This prints:
*
Column names are WeekDay; Misc
On Monday A
On Tuesday B
On Wednesday C
On Thursday D
On Friday E
On Saturday F
On Sunday G
*
Later in program:’
for day_num in range(1, 22):
if day_num == 1:
file.write("~stuff~nn")
if day_num == 8:
dz8 = d + timedelta(days=day_num-1)
file.write("~stuff~nn")
if day_num == 15:
dz15 = d +timedelta(days=day_num-1)
file.write("~stuff~nn")
dz = d + timedelta(days=day_num-1)
if day_num == 1 or day_num == 8 or day_num == 15:
Perf = PerfDay1
print(PerfDay1)
elif day_num == 2 or day_num == 9 or day_num == 16:
print(PerfDay2)
Perf = PerfDay2
elif day_num == 3 or day_num == 10 or day_num == 17:
print(PerfDay3)
Perf = PerfDay3
elif day_num == 4 or day_num == 11 or day_num == 18:
print(PerfDay4)
Perf = PerfDay4
elif day_num == 5 or day_num == 12 or day_num == 19:
print(PerfDay5)
Perf = PerfDay5
elif day_num == 6 or day_num == 13 or day_num == 20:
print(PerfDay6)
Perf = PerfDay6
elif day_num == 7 or day_num == 14 or day_num == 21:
print(PerfDay7)
Perf = PerfDay7
file.write("~stuff~n")
‘This prints:
*
A
*
Any ideas why the values of PerfDay2 thru PerfDay7 become blank?
Upon reusing the variables only the first one contains data, the next 6 are empty.’
Mike Emerson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.