name = company_location
for i, j in enumerate (list):
if j == 'Last 10 Accidents':
last = i
try:
date1 = list[last+28]
employee1 = list[last+31]
days_lost1 = list[last+49]
days_paid1 = list[last+43]
with open("report.csv","a+") as report:
report.write(str(name)+", "+str(date1)+", "+str(employee1)+", "+str(days_lost1)+", "+str(days_paid1))
report.close()
except (ValueError, TypeError, IndexError):
with open("report.csv","a+") as report:
report.write(str(name)+"\n")
report.close()
try:
date2 = list[last+50]
employee2 = list[last+53]
days_lost2 = list[last+71]
days_paid2 = list[last+65]
with open("report.csv","a+") as report:
report.write(", "+str(date2)+", "+str(employee2)+", "+str(days_lost2)+", "+str(days_paid2))
report.close()
except (ValueError, TypeError, IndexError):
with open("report.csv","a+") as report:
report.write("\n")
report.close()
try:
date3 = list[last+72]
employee3 = list[last+75]
days_lost3 = list[last+93]
days_paid3 = list[last+87]
with open("report.csv","a+") as report:
report.write(", "+str(date3)+", "+str(employee3)+", "+str(days_lost3)+", "+str(days_paid3))
report.close()
except (ValueError, TypeError, IndexError):
with open("report.csv","a+") as report:
report.write("\n")
report.close()
2 Answers
for i, j in enumerate (list):
You can't iterate through list
because it is a type, so I am assuming that list
is the name of a variable that you defined earlier on. Don't do that. When you use the name of something built-in for something else, you are overriding the variable that was previously defined. If, for instance, you wanted to iterate through a list of tuples, but you actually wanted a list of lists, you could do for sub in map(list, tuples):
, but with list
redefined, you can't do that. Besides that, it may confuse future readers of your code to be using list
there when you can't iterate through a type. Instead, use a name that not only says what type of object it is, but describes what is inside.
You have quite a bit of duplicate code in that if j == 'Last 10 accidents':
block. You should instead use a for
loop iterating through different starting points. That is, for start in (28, 50, 72):
and use date = list[last + start]
, employee = date[list + start + 3]
, ... You should be using a more descriptive name than list
anyway, but I outlined that above.
with open("report.csv","a+") as report: report.write(str(name)+", "+str(date1)+", "+str(employee1)+", "+str(days_lost1)+", "+str(days_paid1))
Using a long string (no pun intended) of concatenations doesn't seem very good, does it? Instead, use .join()
:
info = (name, date1, employee1, days_lost1)
report.write(", ".join(map(str, info)) + "\n")
That shortens the code and makes it a little easier to see what you are doing.
report.close()
The with
statement will take care of closing the file so you don't need that line.
Your modified program would look like this:
name = company_location
for i, j in enumerate(reports):
if j == 'Last 10 Accidents':
last = i
for start in (28, 50, 72):
try:
date = reports[last + start]
employee = reports[last + start + 3]
days_lost = reports[last + start + 21]
days_paid = reports[last + start + 15]
info = (date, employee, days_lost, days_paid)
with open("report.csv","a+") as report:
report.write(", ".join(map(str, info)) + "\n")
except (ValueError, TypeError, IndexError):
with open("report.csv","a+") as report:
report.write(str(name)+"\n")
-
\$\begingroup\$ Thank you, zondo. That is exactly what I needed. I appreciate you helping me get over my hurdle. \$\endgroup\$ Mar 18, 2016 at 17:12
You would need to :
- add another
for
loop overk
, before thetry
statements which would become unique, - replace the variables such as
datek
by arrays, likedate[k]
, - and the variables such as
list[last+28]
byk
dependent ones, likelist[last+16+k]
.