Archive | October, 2013

Import CSV to mysql

23 Oct

To import an Excel file into MySQL, first export it as a CSV file. Remove the CSV headers from the generated CSV file along with empty data that Excel may have put at the end of the CSV file.

You can then import it into a MySQL table by running:

load data local infile 'uniq.csv' into table tblUniq fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(uniqName, uniqCity, uniqComments)

 

 

 

 

Remove python2.7 and rollback to python2.6

21 Oct
sudo ln -f /usr/bin/python2.6 /usr/local/bin/python





Use different Python version with virtualenv

18 Oct

virtualenv -p /usr/bin/python2.6 /provide/path/to/new/virtualenv/

Where python2.6 is the python version which can be changed to anything and virtualenv path is the folder created by you to install virtualenv in it

mysql retrieve first 100 characters

14 Oct

 

SELECT LEFT(field, 100) AS excerpt FROM table(s) WHERE ...

See the LEFT() function

 

Calculate n-gram in python

12 Oct
def ngrams(sequence, n, pad_left=False, pad_right=False, pad_symbol=None): 
	if pad_left: 
		sequence = chain((pad_symbol,) * (n-1), sequence) 
	if pad_right: 
		sequence = chain(sequence, (pad_symbol,) * (n-1)) 
	sequence = list(sequence)     
    count = max(0, len(sequence) - n + 1) 
    return [tuple(sequence[i:i+n]) for i in range(count)] 


Now lets get Started:

1) Bigrams:
sequence = [1,2,3,4,5]
n=2
ngrams(sequence,n)
[(1,2),(2,3),(3,4),(4,5)]

For Trigrams n=3 and so on..
If you want a left or right padding when the numbers are odd then mention the left Padding or right padding variable

Enjoy.

How to grep inside a folder

12 Oct

This is how we do it

grep -nr string my_directory