I am pretty new to python and coding. I am trying to write a code that will print out the total amount of each given wire type from a list of wires. This is a side project for work. I was able to come up with a code to sum up all of the wire for a user defined wire type. Now I would like to make another code that prints out the total of each wire type in the file.
This is the code that I came up with to sum up individual wire type as selected by the user.
wtype = []
w = []
w1 = []
#opens the .TXT file
fhand = input('\nEnter Text File\n')
try:
if (len(fhand) <= 0):
fhand = 'test.txt'
fh = open(fhand)
except:
print('\nNo File Found:', fhand, '\n')
exit()
#prints out the possible wire types
for line in fh:
line = line.rstrip()
wtype.append(line) #needed for later in the code
line2 = line.split(',')[2]
if line2 not in w:
w.append(line2)
else:
continue
d1 = dict(enumerate(w))
print(d1)
#sums up the selected wire types total length from the given .TXT file
wire = int(input('\nEnter the number that is before the wire type you need:\n'))
for key, val in d1.items():
if key == wire:
for x in wtype:
x = x.split(',')
if x[2] == val:
w1.append(x[1])
else:
continue
s = [eval(i) for i in w1]
print('\nYour will need ', sum(s)/12, ' Feet of ', val, '.\n')
This is the test.txt
file, the length is in inches and the converted to feet in the last line of the code sum(s)/12
:
the column are WIRE, LENGTH, TYPE, QTY for this file.
WIRE-006A22,72,M22759/16-22-9,1
WIRE-005A22,60,M22759/16-22-9,1
WIRE-004A22,72,M22759/16-22-9,1
WIRE-003A22,72,M22759/16-20-9,1
WIRE-002A22,60,M22759/16-20-9,1
WIRE-001A22,72,M22759/16-22-9,1
WIRE-009A22,72,M22759/16-22-9,1
WIRE-008A22,60,M22759/16-22-9,1
WIRE-007A22,72,M22759/16-20-9,1
WIRE-011A22,72,M22759/16-22-9,1
WIRE-012A22,72,M22759/16-22-9,1
WIRE-014A22,72,M22759/16-20-9,1
WIRE-013A22,60,M22759/16-22-9,1
WIRE-021A22,72,M22759/16-20-9,1
WIRE-031A22,72,M22759/16-22-9,1
WIRE-032A22,72,M22759/16-20-9,1
WIRE-043A22,60,M22759/16-22-9,1
WIRE-054A22,72,M22759/16-20-9,1
WIRE-065A22,72,M22759/16-22-9,1
WIRE-076A22,60,M22759/16-22-9,1
WIRE-087A22,72,M22759/16-22-9,1
WIRE-098A22,72,M22759/16-20-9,1
WIRE-089A22,72,M22759/16-20-9,1
WIRE-078A22,72,M22759/16-20-9,1
WIRE-067A22,60,M22759/16-22-9,1
WIRE-056A22,72,M22759/16-22-9,1
WIRE-045A22,72,M22759/16-20-9,1
WIRE-034A22,60,M22759/16-22-9,1
WIRE-023A22,60,M22759/16-22-9,1
WIRE-012A22,72,M22759/16-20-9,1
The output I am looking to try and achieve is:
output: {'M22759/16-22-9': 100, 'M22759/16-20-9': 71}
and have that be expandable to all the different wire types that could be in d1