Advertisement
Guest User

lesswrong post parser

a guest
May 13th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #New lines are fine
  2. #Must have leading and trailing triple-quotes
  3. text = '''Example Post by tgb | 0v (3c)
  4. Another by someone | -1v (0c)
  5. I "hate" the CSV, format by foo | -5v (0c)'''
  6.  
  7. #change this filename to what you want to output to!
  8. output = file("output.csv", "w")
  9.  
  10. lines = text.split("\n")
  11. for l in lines:
  12.     start, end = tuple(l.split(" | "))
  13.  
  14.     title = start[:start.rfind(" by")]
  15.     title = title.replace("\"", "'") #Replace quotes with single quotes
  16.     author = start[start.rfind("by")+3:]
  17.  
  18.     votes = int(end[:end.find("v")])
  19.     comments = int(end[end.find("(")+1:end.find("c")])
  20.  
  21.     line_out  = "\"%s\", \"%s\", %s, %s\n"%(title, author, votes, comments)
  22.     output.write(line_out)
  23. output.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement