2015-07-13

PYTHON - How to parse a JSON string

# 1. Original JSON String
text = """{ "count": 2, "record": [ {"lemma":"God"}, {"lemma":"Men"} ] }"""

# 2. Text to JSON object
import json
j = json.loads(text)

# 3. Parse a top-level property
print 'count: ' + str( j['count'] )

# 4. Traverse multiple records
for item in j['record']:
    print( item['lemma'] )

PYTHON - Break a paragraph into a sentences list

import re
text = 'This is. A paragraph with. Three sentences.'
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
for item in sentences:
    print(item)

PYTHON - Getting text from a website

import requests
txt = requests.get("http://lipsum.com/robots.txt").text
print( txt )


2015-07-09

2015-07-08

PYTHON - Display and change current working directory

import os
print( os.getcwd() )
os.chdir( os.path.dirname("C:/folder/subfolder") )

print( os.getcwd() )