2015-07-17

MongoDB - Import CSV with field values containing double quotes

You have to escape the double quote.
Do not escape it with a backslash (\").
Instead, escape it with another double quote ("").

MongoDB - How to rename a field

db.COLLECTION_NAME.update( {}, { $rename: {"OLD_FIELD_NAME": "NEW_FIELD_NAME"} }, false, true);

MongoDB - How to import a CSV file

mongoimport --db DATABASE_NAME --collection COLLECTION_NAME --type csv --headerline --file /path/to/file/FILE_NAME.csv

MacOS - Show hidden files and folders

defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder

2015-07-15

PYTHON - Randomly choose an list/array element (numeric or text)

import random
print( random.choice( [1,2,3] ) )
print( random.choice( ['a','b','c'] ) )

2015-07-13

PYTHON - Twitter search and results parsing

# 1. Twitter authentication credentials (get yours at dev.twitter.com)
consumerKey = ''
consumerSecret = ''

# 2. Create a twython object (installation command: "sudo easy_install twython")
from twython import Twython
twitter = Twython( consumerKey, consumerSecret )

# 3. Display tweets' text matching a given search term
term = 'chapo'
for status in twitter.search(q=term)["statuses"]:  
    print status["text"]

PYTHON - How to most efficiently concatenate strings

from cStringIO import StringIO
string = StringIO()

string.write( 'First sentence.' )
string.write( 'Second string.' )
string.write( 'Thrid' )

print( string.getvalue() )