Jan-31-2020, 05:59 PM
so i had a text file which had data on seperate lines and i had to use the mod method to take out the desired values and convert it into a list(as the data is in consistent order) but it keeps throwing an error.Oh and the data is repeated after every 5 lines, like first is the id then name then earnings etc
['12344', '', '', '', '', '98123', '', '', '', '', '32145', '', '', '', '', '']
[0, 0, 0, 40, 0, 0, 0, 0, 34, 0, 0, 0, 0, 30, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
the data is present in the string(except the 3rd) but there are all the unnecessary bits)
where did i go wrong!
f= open("my employees.txt") emp_name = ['']*99999 hours_spent = [0]*99999 per_hours_salary = [0]*99999 index = 0 line = 0 while True: rough = f.readline() line += 1 length = len(rough)-1 if rough == '': break if line % 5 == 1: emp_name[index] = rough[:length] line += 1 elif line % 5 == 2: per_hours_salary[index] = int(rough[:length]) line += 1 elif line & 5 == 3: hours_spent[index] = int(rough[:length]) line += 1 else: line += 1 index += 1 emp_name[index+1:] = [] per_hours_salary[index+1:] = [] hours_spent[index+1:] = [] print(emp_name) print(per_hours_salary) print(hours_spent)the output is:
['12344', '', '', '', '', '98123', '', '', '', '', '32145', '', '', '', '', '']
[0, 0, 0, 40, 0, 0, 0, 0, 34, 0, 0, 0, 0, 30, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
the data is present in the string(except the 3rd) but there are all the unnecessary bits)
where did i go wrong!