Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import os
  3. import csv
  4. import pandas
  5.  
  6.  
  7. def extract_lines_from_files(filename, dirpath):
  8. filename = os.path.join(dirpath, filename)
  9.  
  10. search_keywords = ['Apple','Pear','Cherry'] # 3 Columns
  11.  
  12. with open(filename, 'r',errors='ignore') as Text_file:
  13.  
  14. for line in Text_file:
  15. found = False
  16. for word in search_keywords:
  17. if word in line:
  18. found = True
  19. if found:
  20. lines = []
  21. lines.append(line)
  22.  
  23. with open("a.csv",'a') as csv_file:
  24. writer = csv.writer(csv_file)
  25. writer.writerow([line])
  26.  
  27. #main
  28. directory = 'C:/Users/home/Desktop/files/'
  29. for root, dirs, files in os.walk(directory):
  30. for f in files:
  31. extract_lines_from_files(f, directory)
  32. #done
  33.  
  34. Apple 1 | Pear 1 | Cherry 1
  35.  
  36. Apple 2 | Pear 2 | Cherry 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement