Reputation: 1
Hello i'm new to python. I need to open a .csv archive(a list of ER admissions) read it and then save it into a .txt file. After that, I need to search for any record that includes any "death" and print it. I know how to open the csv and read it, but no clue how to save it into a txt. And the second part is giving me some trouble
archivo = open('path/.csv', 'r+')
for linea in archivo.readlines():
archivo.write(linea.replace(',', ' ')) #I should be saving into a .txt here, open the txt and then
if "DEATH" in file:
print line
I want the result to be the whole info(line)asociated with "DEATH" not just the word 'death'
Upvotes: 0
Views: 51
Reputation: 501
you can do like this..
f = open('yourTxtFile.txt','w')
with open('your_csv_file.csv', 'r+') as file_obj:
for line in file_obj.readlines():
f.write(line)
if "death" in line.lower():
print line
f.close()
if you open a file as your way you have to close file. so you can use with open statement to open a file where in you need not have to worry to close the file.
Upvotes: 0