Guest User

Untitled

a guest
Dec 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. #%%
  2. import os
  3. import re
  4.  
  5. os.chdir('data_directory')
  6. raw_files = [x for x in os.listdir()]
  7.  
  8. for raw in raw_files:
  9. inputfile = raw
  10. outputfile = raw.split('.')[0]+'_clean.txt'
  11.  
  12. with open(inputfile, 'r') as f:
  13. lines = f.readlines()
  14. lines = [x.strip('\n') for x in lines]
  15.  
  16. data_rows = []
  17. for line in lines:
  18. cleanline = re.sub('\s+', ' ', line).strip()
  19. data_rows.append(cleanline)
  20.  
  21. with open(outputfile, 'a') as f:
  22. for obs in data_rows:
  23. f.write(obs + '\n')
  24. f.close()
Add Comment
Please, Sign In to add comment