how to save csv into a txt, and search for records
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'
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.
链接地址: http://www.djcxy.com/p/55100.html上一篇: 从Python中的电子邮件中提取文本
下一篇: 如何将csv保存到txt中,并搜索记录