Recent Notes

Displaying keyword search results 1 - 10
Created by woolf on May 20, 2011 14:00:35    Last update: May 20, 2011 14:01:36
To remove new line characters from a file: with tr : tr -d '\n' < the-file.txt or cat the-file.txt | tr -d '\n' with sed : sed ':a;N;$!ba;s/\n//g' the-file.txt or cat the-file.txt | sed ':a;N;$!ba;s/\n//g' The sed version is a bunch of commands to manipulate the register etc. Regex replace " s/\n//g " does not work because sed regex works on a single line. More tips from this Linux blog: http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/
Created by Dr. Xi on September 10, 2010 20:58:34    Last update: September 10, 2010 20:58:34
From Python docs : string.find(s, sub[, start[, end]]) Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end] . Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices. string.index(s, sub[, start[, end]]) Like find() but raise ValueError when the substring sub is not found. >>> s = 'abcd1234' >>> s.index('123') 4 >...
Created by Dr. Xi on September 10, 2010 20:52:32    Last update: September 10, 2010 20:52:32
Unlike PHP, in Python an array index must be integer (string is an array of characters): >>> s 'abcd12345' >>> s[1] 'b' >>> s['...
Created by Dr. Xi on August 30, 2010 18:17:15    Last update: August 30, 2010 18:19:49
Use the codecs module to read file in Unicode. This is from the Python doc : import codecs f = codecs.open('unicode.rst', en... I had some luck reading files mainly in ASCII but contained some binary data with: import codecs f = codecs.open('unicode.rst', en...
Created by Dr. Xi on August 30, 2010 16:33:46    Last update: August 30, 2010 16:33:46
For Python 2.6 and later, this: with open(filename) as logfile: for line in... is equivalent to this: logfile = open(filename) try: for line i...
Created by Dr. Xi on August 30, 2010 15:58:29    Last update: August 30, 2010 15:58:29
Python String does not have functions like substring or substr . Instead, substrings are specified with the slice notation : two indices separated by a colon. >>> s = 'readme.txt' >>> s.substring(0, 3) T...
Created by Dr. Xi on January 04, 2010 05:04:10    Last update: January 07, 2010 15:59:25
This is the error: >>> import urllib2 >>> f = urllib2.urlopen('htt... Reason: SSL is not supported in Python installation. >>> import httplib >>> hasattr(httplib, 'HTTPS'... Solution: recompile Python with SSL on Steps: Download and install OpenSSL , if you don't have it already. Download Python source and rebuild Python (the usual steps of configure, make and make install). Python's configure script should be able to pick up your existing SSL libraries automatically and build a shared library _ssl.so. Some web sites suggest editing the file Modules/setup.dist , uncomment the lines starting with _ssl , and making changes to the SSL path. This would link the SSL library statically to Python. # Socket module helper for socket(2) #_socket s...
Created by Dr. Xi on January 02, 2010 16:37:13    Last update: January 02, 2010 16:37:57
Use the del operator to delete one key, or clear method to delete all keys: >>> d = { 'a': 1, 'b': 2, 'c' : 3 } >>> del d['...
Created by Dr. Xi on May 05, 2009 20:06:16    Last update: May 05, 2009 20:06:38
Write to a file, overwrite if it exists: f = open ('test.txt', 'w') f.write ('li... Append to a file: f = open ('test.txt', 'a') f.write ('li...
Created by Dr. Xi on May 05, 2009 19:59:21    Last update: May 05, 2009 19:59:21
Delete a single file: >>> import os >>> file = 'myfile' >>> os.rem... Delete a directory and contents: >>> import shutil >>> dir(shutil) ['Error', ...
Previous  1 2 Next