SHARE
TWEET

Untitled

a guest Jun 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ID:- 512
  2. NAME:- GOOSE
  3. PROJECT NAME:- Random
  4. REPORT ID:- 30321
  5. REPORT NAME:- ANIMAL
  6. KEYWORDS:- ['"help,goose,Grease,GB"']
  7.  
  8.  
  9. ID:- 566
  10. NAME:- MOOSE
  11. PROJECT NAME:- Random
  12. REPORT ID:- 30213
  13. REPORT NAME:- ANIMAL
  14. KEYWORDS:- ['"Moose, boar, hansel"']
  15.      
  16. ID, NAME, PROJECT NAME, REPORT ID, REPORT NAME, KEYWORDS
  17.      
  18. with open('test.txt', 'r') as in_file:
  19.     stripped = (line.strip() for line in in_file)
  20.     lines = (line for line in stripped if line)
  21.     grouped = itertools.izip(*[lines] * 6)
  22.     with open('extracted.csv', 'w') as out_file:
  23.         writer = csv.writer(out_file)
  24.         writer.writerow((‘ID’, ‘NAME’, ‘PROJECT NAME’, ‘REPORT ID’, ‘REPORT NAME’, ‘KEYWORDS’))
  25.         writer.writerows(grouped)
  26.      
  27. import re
  28.  
  29. # `text` is your input text
  30.  
  31. data = {}
  32. keys = ['ID', 'NAME', 'PROJECT NAME', 'REPORT ID', 'REPORT NAME', 'KEYWORDS']
  33. for k in keys:
  34.     data[k] = re.findall(r'%s:- (.*)' % k, text)
  35.      
  36. import csv
  37.  
  38. csv_file = 'out.csv'
  39.  
  40. with open(csv_file, 'w') as csvfile:
  41.     writer = csv.DictWriter(csvfile, fieldnames=keys)    
  42.     writer.writeheader()
  43.     for x in data:
  44.         writer.writerow(x)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top