I found that reading a single column file with strings is not that easy in python.
When I want the following function to read each line of the file provided and do what I want it returns an empty data frame. If I call the function with number of lines of the file – 1 then it returns the data frame with data, but I lose the last line.
def xToy(fname, typ, nlines):
i = 0
x1 = [None] * nlines
x2 = [None] * nlines
x3 = [None] * nlines
dfm = pd.DtaFrame({'xSrt': x1, 'ySrt': x2, 'typ': x3})
with open(fname, 'r') as read_obj:
lines = read_obj.readlines()
for line in lines:
if i < nlines - 1 :
line = line.strip()
y = yfromx(line)
x1[i] = line
x2[i] = y
x3[i] = typ
else:
dfm['xSrt'] = x1
dfm['ySrt'] = x2
dfm['typ'] = x3
return dfm
i += 1
If I call the function with actual number of lines (112) in the file
dp = xToy("file.xyz", 1, 112)
and then if I do the following, I get “none”
print(dp)
if I call the function with number of lines in the file – 1
dp = xToy("file.xyz", 1, 111)
then
print(dp)
shows data in dp.
This means my function fails to process the last line of the file and if I provide number of lines -1 as nlines then I get the data, but I lose the last line.
To address this issue, I did the following and it works
def xToy(fname, typ, nlines):
i = 0
x1 = [None] * nlines
x2 = [None] * nlines
x3 = [None] * nlines
dfm = pd.DtaFrame({'xSrt': x1, 'ySrt': x2, 'typ': x3})
with open(fname, 'r') as read_obj:
lines = read_obj.readlines()
for line in lines:
if i < nlines - 1 :
line = line.strip()
y = yfromx(line)
x1[i] = line
x2[i] = y
x3[i] = typ
else:
line = line.strip()
y = yfromx(line)
x1[i] = line
x2[i] = y
x3[i] = typ
dfm['xSrt'] = x1
dfm['ySrt'] = x2
dfm['typ'] = x3
return dfm
i += 1
Is there a smart way to do the same?
Yes, as mkrieger1 pointed out, I failed to spot that my return was inside the for loop. If I bring my return out as shown below, everything works as expected.
def xToy(fname, typ, nlines):
i = 0
x1 = [None] * nlines
x2 = [None] * nlines
x3 = [None] * nlines
dfm = pd.DtaFrame({'xSrt': x1, 'ySrt': x2, 'typ': x3})
with open(fname, 'r') as read_obj:
lines = read_obj.readlines()
for line in lines:
if i < nlines - 1 :
line = line.strip()
y = yfromx(line)
x1[i] = line
x2[i] = y
x3[i] = typ
else:
dfm['xSrt'] = x1
dfm['ySrt'] = x2
dfm['typ'] = x3
i += 1
return dfm
3