In [ ]:
# read from file
with open("data/mydata.txt") as f:
for line in f:
print(line.strip())
In [ ]:
# write to file
with open( "out.txt", "w" ) as out:
out.write("hello")
In [ ]:
# example in data/mydata.txt (tab delimited)
# read into a dictionary (more convenient structure)
results = []
with open('data/mydata.txt') as f:
header = f.readline()
for line in f:
idx, org, score = line.split()
#print(idx, org, score)
row = {'index': int(idx), 'organism': org, 'score': float(score)}
#print(row)
results.append(row)
print(results)
In [ ]:
# write results into a comma separated file
with open('mydata.csv', 'w') as out:
out.write('index,organism,score\n')
for r in results:
out.write('{},{},{}\n'.format(r['index'], r['organism'], r['score']))