I'm trying to parse a lot of files to get some info in one table. Here is my script:
import pandas as pd
import numpy as np
M1 = pd.DataFrame(Test,columns=['Test'])
for sample in samples:
with open("file/"+sample+".txt") as f:
c=0
tpm=[]
for line in f:
c+=1
# if c==1:
# if line.split('/')[1].split('.')[0]!='abc':
# break
if c>2 and line.startswith('gene'):
try:
return tpm.append(int(line.rstrip().split('\t')[6])/int(line.rstrip().split('\t')[5])*1000)
except ZeroDivisionError:
return 0
M1[Test]=tpm/np.sum(tpm)*1000000
M=M1
M=M.fillna(0)
M.index=M['Test']
M.to_csv('M.xls',index=False,sep='\t')'
Without lines containing try:
and except ZeroDivisionError
it works but I got a ZeroDivision error and results are not usable for the next line. So I wanted to add 0 when a 0 is encountered in *.txt file instead of doing division.
In this script I got a 'return' outside function
syntax error
I tried many approaches such as add try
before for line in f:
or add except: pass
but it didn't work.