IndexError due to empty lines from input data - python

Below is a part of my python script, which reads data in daily automation in Linux system & print it in mail body & sends. my input file changes daily and it works perfectly if input file contains all strings (or) numarical values.
If the input file has empty string/value in any of rows, it throws IndexError and stops printing the data.
f = open('INPUTfile')
lines = f.readlines()
count=len(lines)
f.close()
body1="""
"""
z=0
while (z<count):
test = lines[z]
hello = test.split(',')
a = hello[0:1]
a1 = a[0]
b = hello[1:2]
b1 = b[0]
c = hello[2:3]
c1 = c[0]
d = hello[3:4]
d1 = d[0]
e = hello[4:5]
e1 = e[0]
f = hello[5:6]
f1 = f[0]
g = hello[6:7]
g1 = g[0]
h = hello[7:8]
h1 = h[0]
i = hello[8:9]
i1 = i[0]
j = hello[9:10]
j1 = j[0]
k = hello[10:11]
k1 = k[0]
l = hello[11:12]
l1 = l[0]
m = hello[12:13]
m1 = m[0]
d1 = float(d[0])
g1 = float(g[0])
j1 = float(j[0])
m1 = float(m[0])
if all([d1 < 99.00, j1 < 99.00]):
body1 = body1 + '<tr><td style="font-family:Calibri;"><b>' + a1 + '</b></td><td style="font-family:Calibri;">' + b1 + '</td></td><td style="font-family:Calibri;">' + c1 + '</td></td><td style="font-family:Calibri;color:red">' + str(round(d1,2)) + '</td></td><td style="font-family:Calibri;">' + e1 + '</td><td style="font-family:Calibri;">' + f1 + '</td></td><td style="font-family:Calibri;color:red">' + str(round(g1,2)) + '</td><td style="font-family:Calibri;">' + h1 + '</td><td style="font-family:Calibri;">' + i1 + '</td><td style="font-family:Calibri;">' + str(round(j1,2)) + '</td><td style="font-family:Calibri;">' + k1 + '</td><td style="font-family:Calibri;">' + l1 + '</td><td style="font-family:Calibri;">' + str(round(m1,2)) + '</td></tr>'
z=z+1
My inputfile:
APPU1,2004423,2004417,99.9997,2847,2847,100,7600,7599,99.9846,1248,1248,99.9999
APPU2,,,
APPU3,2004333,2004329,99.9998,2848,2848,100,7593,7592,99.9842,1248,1247,99.9999
APPU4,2004020,2004016,99.9998,2849,2847,100,7596,7595,99.9853,1248,1247,99.9999
please suggest solution to print the data even if the rows in INPUT file contains null values.

I don't understand use of while loop here.
Anyway what you need is an if statement at the starting of while loop.
while (z<count):
test = lines[z]
hello = test.split(',')
if len(hello) < 14: # or whatever number of items required.
z+=1
continue
#rest of your code goes here
If I were you, I would write the code like this.
with open('INPUTfile') as f:
for i, line in enumerate(f):
hello = line.split(',')
#rest of the code.

You can use try except block in python.
try:
<your logic here> or the code
except IndexError:
pass # If IndexError is encountered, pass the control to the loop and continue from the next line
Use this code snippet and check if that solves the problem
for line in lines:
try:
test = line
hello = test.split(',')
a = hello[0:1]
a1 = a[0]
b = hello[1:2]
b1 = b[0]
c = hello[2:3]
c1 = c[0]
d = hello[3:4]
d1 = d[0]
e = hello[4:5]
e1 = e[0]
f = hello[5:6]
f1 = f[0]
g = hello[6:7]
g1 = g[0]
h = hello[7:8]
h1 = h[0]
i = hello[8:9]
i1 = i[0]
j = hello[9:10]
j1 = j[0]
k = hello[10:11]
k1 = k[0]
l = hello[11:12]
l1 = l[0]
m = hello[12:13]
m1 = m[0]
d1 = float(d[0])
g1 = float(g[0])
j1 = float(j[0])
m1 = float(m[0])
except IndexError:
pass

Related

How to update row/column cell value given an index?

I'm trying to overwrite a columns of 1's if there are duplicates but for some reason this code won't >overwrite the 1's, it does something else.
Sample input
dhid,midx,midy,midz,a,dtype
AAA1,321.235,200.436,59.72,7,RR
AAA7,321.235,200.436,59.72,-99,CR
AAA2,321.235,200.236,68.7,15,CR
Example Output:
dhid,midx,midy,midz,a,dtype,KEEPVA
AAA1,321.235,200.436,59.72,7,RR,1
AAA7,321.235,200.436,59.72,-99,CR,0 --> GETS RID OF THIS DUPLICATE THAT IS MISSING
AAA2,321.235,200.236,68.7,15,CR,1
My try so far
dup_dict = []
flg = False
for i in range(npts-1):
d2 = 0.0
for idir in range(2,-2,-1):
if (idir==-1 and d2 <= d2tol ):
dup_dict.append([i,i+1,d2])
break
d = np.abs(xyz[i,idir]-xyz[i+1,idir])
if (d > dtol):
break
d2 = d2 + d ** 2
print('Total Duplicates Found: %d'%(len(dup_dict)))
df['KEEPVA'] = np.ones(len(df.index))
fmtlist = ['%s','%0.5f','%0.5f','%0.5f','%0.5f','%d','%s']
fl = open('dup_gq.log','w')
fl.write(','.join(['dhid','midx','midy','midz','a','KEEPVA','dtype'])+'\n')
for i0,i1,d in dup_dict:
aufa0 = df['a'].values[i0]
aufa1 = df['a'].values[i1]
dtype0 = df['dtype'].values[i0]
dtype1 = df['dtype'].values[i1]
if (dtype0!=dtype1):
print(df['dhid'].values[i0],df['dhid'].values[i1],dtype0,dtype1,a0,a1)
if a0 > a1:
df.loc[i1,'KEEPVA'] = 0
if a0 <= a1:
df.loc[i0,'KEEPVA'] = 0
print(df['dhid'].values[i0],df['dhid'].values[i1],df['KEEPVA'].values[i0],
df['KEEPVA'].values[i1])
a = df[['dhid','midx','midy','midz','a','KEEPVA','dtype']].values[[i0,i1],:]
np.savetxt(fl,a,fmt=fmtlist,delimiter=',')
fl.close()

4th order Runge-Kutta for infinite square well

The following code works for n = 1 but for higher energy levels the wave function is flipped in the x-axis compared to the expected wave function. Does anyone know why this is? V(x) is set to be 0 for all x and the energies were all correct compared to expected values. All constants have been defined.
x = np.arange(-a,a,h)
def f(r,x,E):
psi = r[0]
phi = r[1]
fpsi = phi
fphi = (2*m/(hbar**2))*(V(x)-E)*psi
return np.array([fpsi,fphi],float)
def RungeKutta2d(xpoints,E):
r = [0,1]
psipoints = []
phipoints = []
for x in xpoints:
psipoints.append(r[0])
phipoints.append(r[1])
k1 = h*f(r,x,E)
k2 = h*f(r+0.5*k1, x+0.5*h,E)
k3 = h*f(r+0.5*k2, x+0.5*h,E)
k4 = h*f(r+k3, x+h,E)
r = r + (k1 + 2*k2 + 2*k3 + k4)/6
psipoints.append(r[0])
phipoints.append(r[1])
return np.array(psipoints)
def Secant(E1,E2):
psi1 = RungeKutta2d(xpoints,E1)[N]
psi2 = RungeKutta2d(xpoints,E2)[N]
tolerance = e/1000
while abs(E2-E1) > tolerance:
E3 = E2 - psi2*(E2-E1)/(psi2-psi1)
E1 = E2
E2 = E3
psi1 = RungeKutta2d(xpoints,E1)[N]
psi2 = RungeKutta2d(xpoints,E2)[N]
return E3

IndexError: list assignment index out of range. How do I solve this?

I am trying the Newmark's constant average acceleration method. I am getting this error. How do I recover from this error?
IndexError Traceback (most recent call last)
41 for i in range(len(t)):
42 pn[i+1] = p[i+1]+ a1*u[i] + a2*v[i] + a3*a[i]
43 u[i+1] = pn[i+1]/kn
44 v[i+1] = y*(u[i+1]-u[i])/(b*dt) + (1-y/b)v[i] + dt (1-y/(2*b))*a[i]
IndexError: list assignment index out of range
y = 1/2
b = 1/4
u = []
v = []
t = []
p = [0,25,43.3013,50,43.3013,25,0,0,0,0,0,0]
a = []
pn = []
pn.append(0)
x = 0.0
for i in range(11):
z = 0.0 + x
t.append(z)
x = x + 0.1
m = 0.45594
k = 18
c = 0.2865
u.append(0)
v.append(0)
a.append((p[0]-c*v[0]-k*u[0])/m)
dt = 0.1
a1 =(m/(b*dt*dt)+y*c/(b*dt))
a2 = (m/(b*dt)+(y/b-1)*c)
a3 = (((1/(2*b))-1)*m + dt*((y/(2*b))-1)*c)
kn = k + a1
for i in range(len(t)-1):
pn[i+1] = p[i+1]+ a1*u[i] + a2*v[i] + a3*a[i]
u[i+1] = pn[i+1]/kn
v[i+1] = y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i]
a[i+1] = (u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i]
Your pn, a, u, v are defined as a list with length 1, so there is no index such as pn[1]. You can use append or define the list with needed length.
for i in range(len(t)):
pn.append(p[i+1] + a1*u[i] + a2*v[i] + a3*a[i])
u.append(pn[i+1]/kn)
v.append(y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i])
a.append((u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i])
or
pn, a, u, v = [0]*11, [0]*11, [0]*11 [0]*11
pn[0], u[0], v[0] = 0, 0, 0
a[0] = (p[0]-c*v[0]-k*u[0])/m
...
for i in range(len(t)-1):
pn[i+1] = p[i+1] + a1*u[i] + a2*v[i] + a3*a[i]
u[i+1] = pn[i+1]/kn
v[i+1] = y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i]
a[i+1] = (u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i]

Errors with sha-256 python implementation

Over the last few days I have been studying how hashing algorithms like SHA-256 actually work. To get a better understanding for the process I decided to implement it in python (3.6). By stitching together concepts from various websites like wiki and NIST pages I constructed the following code. I know the process is close to what it should be but when I test it results vary slightly from what they should be. For example, 'a' hashed with SHA-256 should be 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb' however, my program returns 'ca978112ca1bbdcafac231b319a23dc4da786eff81147c4e72b9807785afee48bb'. The answers are very similar but are not the same. What am i doing wrong. Thank you for your time looking into it.
def hash(string):
return(process(pad(string)))
def pad(string):
data = ""
length = len(string) * 8
for c in string:
data += bin(ord(c))[2:].zfill(8)
data += "1"
while len(data)%512 != 448:
data += "0"
data += bin(length)[2:].zfill(64)
return data
def process(bins):
main_block=[
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
h0 = 0x6a09e667
h1 = 0xbb67ae85
h2 = 0x3c6ef372
h3 = 0xa54ff53a
h4 = 0x510e527f
h5 = 0x9b05688c
h6 = 0x1f83d9ab
h7 = 0x5be0cd19
for c in chunks(bins, 512):
words = chunks(c, 32)
w = [0]*64
w[:15] = [int(n, 2) for n in words]
for i in range(16, len(w)):
tmp1 = rightRotate(w[i-15], 7) ^ rightRotate(w[i-15], 18) ^ rightShift(w[i-15], 3)
tmp2 = rightRotate(w[i-2], 17) ^ rightRotate(w[i-2], 19) ^ rightShift(w[i-2], 10)
w[i] = (w[i-16] + tmp1 + w[i-7] + tmp2) & 0xffffffff
a = h0
b = h1
c = h2
d = h3
e = h4
f = h5
g = h6
h = h7
for i in range(0, 64):
s1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)
ch = g ^ (e & (f ^ g))
tmp1 = h + s1 + ch + main_block[i] + w[i]
s0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)
maj = (a&b) ^ (a&c) ^ (b&c)
tmp2 = s0 + maj
h = g
g = f
f = e
e = d + tmp1 & 0xffffffff
d = c
c = b
b = a
a = tmp1 + tmp2 & 0xffffffff
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
h5 += f
h6 += g
h7 += h
return '%08x%08x%08x%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4, h5, h6, h7)
def rightShift(x, n):
return (x & 0xffffffff) >> n
def rightRotate(x, y):
return (((x & 0xffffffff) >> (y & 31)) | (x << (32 - (y & 31)))) & 0xffffffff
def chunks(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
string = "a"
print(hash(string))
You are failing to mask out h* after calculating, resulting in spurious leading "1"s.
h0 &= 0xffffffff
...
h7 &= 0xffffffff

Cant make a txt output at proper format

This is my code. The problem is that the output looks like this
2015-06-03 19:32:11.225085
{'2015-01-21-20:56:45.mp3': 1}{'negative': -2}{'2015-01-15-21:28:23.mp3': 1}
i want to be like a dictionary.... like this one below so i can read it back as a dictionary and remove the keys from the first subset sum and go on output a second on and so on until no other subset sum exists...
2015-06-03 19:32:11.225085
{'2015-01-21-20:56:45.mp3': 1, 'negative': -2, '2015-01-15-21:28:23.mp3': 1}
Any ideas?
thanx in advanced
import os, sys,re,gzip, pickle
from itertools import combinations
import json
from datetime import datetime
mp3folder = raw_input('Please copy paste the mp3s path:')
lowerin = input('Please enter your total playlist time in NEGATIVE seconds and hit ENTER:')
r = {}
drk = os.listdir(mp3folder)
drifiles = list(drk)
r = dict.fromkeys(drifiles, 0)
for key in r.keys():
print ('Please enter the duration of...')
print(key)
r[key] = input('in seconds and hit ENTER:')
r['negative'] = lowerin
d = {}
neg = 0
pos = 0
dates = datetime.now()
dates = str(dates)
f = open("dict.txt",'ab')
f.write('\n'+dates+'\n')
f.close()
for (w,v) in r.iteritems():
if v > 0: pos += v
else: neg += v
sums = [0] * (pos - neg + 1)
for (w,v) in r.iteritems():
s = sums[:]
if not s[v - neg]: s[v - neg] = (w,)
for (i, w2) in enumerate(sums):
if w2 and not s[i + v]:
s[i + v] = w2 + (w,)
sums = s
if s[-neg]:
for x in s[-neg]:
d = dict([(x, r[x])])
file('dict.txt','a'+'\n').write(repr(d))
break
f = open('dict.txt','r')
filedata = f.read()
f.close()
newdata = filedata.replace("}{",", ")
f = open('lexiko.txt','w')
f.write(newdata)
f.close()
di = eval(open("lexiko.txt").read())
print di
this will do it

Categories

Resources