import codecs
file = codecs.open("file_with_unicode_data.txt", "r", "utf-8")
print file.readlines()
file.close()
Sunday, March 11, 2012
Reading unicode data through Python
Processing files containing unicode based characters requires using the codec library instead of the standard file processing libraries. The relevant code:
Difference between two folders
Redirect output of difference between two directories, removing the system files (DS_Store, etc..):
diff -qr dir1 dir2 | grep -v -e 'DS_Store' -e 'Thumbs' | sort > diffs_dir1_dir2.txt
diff -qr dir1 dir2 | grep -v -e 'DS_Store' -e 'Thumbs' | sort > diffs_dir1_dir2.txt
Saturday, January 7, 2012
Specify the location with Wget?
-P prefix
--directory-prefix=prefix
Set directory prefix to prefix. The directory prefix is the
directory where all other files and sub-directories will be
saved to, i.e. the top of the retrieval tree. The default
is . (the current directory).
wget -P /home/username/location URL
Thursday, January 5, 2012
Add a column to an existing MySQL table
To add a column called new_column to a table called table_name with a datatype of VARCHAR(20), use the following SQL statement:
ALTER TABLE table_name ADD new_column VARCHAR(20);
This statement will add the new column new_column to the end of the table. To insert the new column after a specific column, such as old_column, use this statement:
ALTER TABLE table_name ADD new_column VARCHAR(20) AFTER old_column;
To add the new column as the first column, we can use the following mysql statement:
ALTER TABLE table_name ADD new_column VARCHAR(60) FIRST;
Wednesday, January 4, 2012
Split String Multiple Delimiters Python
The python function split([sep,[,maxsplit]]) is a good way to split a string for a given delimiter or sep. The delimiter may itself be composed of multiple characters. However, this function is insufficient when multiple delimiters are considered.
>>> l = "Birds;and,Bees"
>>> l.split(";,")
['Birds;and,Bees']
>>> import re
>>> re.split(";|,",l)
['Birds', 'and', 'Bees']
>>> l = "Birds;and,Bees"
>>> l.split(";,")
['Birds;and,Bees']
The way to achieve this is to use the regular expression package and use the re.split(pattern, string, maxsplit=0, flags=0) method available from the package.
>>> import re
>>> re.split(";|,",l)
['Birds', 'and', 'Bees']
The key is to use the "|" operator to indicate multiple delimiters.
Wednesday, November 9, 2011
Amazing Free Software
Dia: open source software to create process diagrams and flowcharts.
AVG Anti-virus: AVG (compared to Norton) is a real good alternative which is free and does not tax the system as much as Norton does. The latest version of AVG also comes with several new features such as Anti- spyware, Anti-rootkit and more.
CCleaner: A cleaning robot for the computer, which is smart enough to know which places to look for unwanted files (e.g. temporary files) and traces. CCleaner can also cleanup several applications along cleaning temporary and unwanted files created by Windows.
Recuva: To recover deleted files, Recuva is one of the best free and reliable alternative that is available today.
Miro: Allows to convert PC into a Internet TV and watch any Internet video channels. YouTube videos and almost any type of files including HD videos can also be played.
Audacity: One of the best free tools available for audio editing and recording for several operating systems. Live audio recording, tapes conversion into digital recordings, cut, copy, splice or mix sounds together and more can be done.
Notepad++: Not just a mere notepad replacement, but allows modification of several file types and supports additional features like auto complete.
Handbrake: A DVD Ripper that rips DVD files and convert them to MPEG-4 format for Mac OS, Linux and Windows.
Foxit PDF Reader: Best alternative to the Adobe PDF reader.
7Zip: Best open source software to manage several types of archives.
xPlorer2 Lite: Very handy utility when it comes to managing files in Windows. The tabbed interface saves you lot of times and hassles simplifying the task of managing files.
Launchy: Open Source software for Windows similar to QuickSilver.
Windows Live Writer: Offline Blog Writer. Manage multiple weblogs and post to them with ease, save drafts online and offline, edit images, etc.
Monday, July 25, 2011
Python Module Not Found
Ways to debug when a python code can't find a module:
Open a terminal and start python. Enter:
from imp import *
find_module(module)
See if the path name corresponds with where your module was supposed to be installed or if the path name directs to a __init__.py file
For Example, we were trying to install pyfacebook and our code won't load the module.
find_module(module) was resulting in the following output:
(<open file 'facebook.py', mode 'U' at 0xb752d4f0>, 'facebook.py', ('.py', 'U', 1))
However, the module was installed at (found using a find command):
/usr/lib/python 2.7/site-packag es/pyfacebook-1 .0a2-py2.7.egg
Clearly, when the code was calling from facebook import *, the code was unable to locate __init__.py file and was crashing. To solve the problem, move the files to /usr/lib/python 2.7/site-packag es/ and see if a further run of find_module points to the required directory.
Open a terminal and start python. Enter:
from imp import *
find_module(module)
See if the path name corresponds with where your module was supposed to be installed or if the path name directs to a __init__.py file
For Example, we were trying to install pyfacebook and our code won't load the module.
find_module(module) was resulting in the following output:
(<open file 'facebook.py', mode 'U' at 0xb752d4f0>, 'facebook.py', ('.py', 'U', 1))
However, the module was installed at (found using a find command):
/usr/lib/python
Clearly, when the code was calling from facebook import *, the code was unable to locate __init__.py file and was crashing. To solve the problem, move the files to /usr/lib/python
Subscribe to:
Posts (Atom)