Dec 222009

Use writelines()
e.g.

# read lines in a file
import sys
arg1 = sys.argv[1]
f = open(arg1, ‘r’)
lines = f.readlines()
f.close()
arg=’out_’+arg1
FILE=open(arg,’w')
for line in lines:
words = line.split(’ ‘)
if words[1] == ‘hypnos’:
FILE.writelines(line)
FILE.close()

Posted by weboom at 2:53 pm Tagged with: ,
Dec 222009

When you want to find a word in a string, use find()
e.g.

import sys
arg1 = sys.argv[1] # passing the first argument
if arg1.find(”off”) == -1: # if arg1 does not contain “off”
# do something
else:
# do something

Posted by weboom at 2:36 pm Tagged with: ,
Dec 222009

Use readlines()
For example,

arg = “filename”

f = open(arg, ‘r’)

lines = f.readlines()
f.close()

Posted by weboom at 2:32 pm Tagged with: ,
Nov 292009

>>>import os
>>>os.chdir(’..’)
>>>os.chdir(’/home/weboom/’)

Posted by weboom at 9:59 pm Tagged with: , ,
Aug 042009

Then,
import datetime

Posted by weboom at 7:49 am Tagged with: , ,
Aug 022009

If you don’t want to refer to the elements of a collection by their position number but prefer some other type of object as a key,
use a python dictionary
———–
e.g.,
>>> tel = {’jack’: 4098, ’sape’: 4139}
>>> tel['guido'] = 4127
>>> tel
{’sape’: 4139, ‘guido’: 4127, ‘jack’: 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{’guido’: 4127, ‘irv’: 4127, ‘jack’: [...]

Posted by weboom at 7:18 pm Tagged with: , , ,
Aug 022009

Python list and tuple, both of them consists of multiple objects identified its position.
list
L=[1, 2, 3]
tuple
T=(1,2,3)
Difference between them is that tuple is immutable.
That is,
L[0]=100
is OK, but
T[0]=100
is not allowed.

Posted by weboom at 7:12 pm Tagged with: , , ,
Aug 022009

Python tuple
consist of a number of objects which can be referenced by their position number within the object.
———
e.g.,
fruit = (”Apple”,”Banana”,”Cherry”,”Fig”,”Grapefruit”)

Posted by weboom at 7:09 pm Tagged with: ,
Aug 022009

__unicode__
is for displaying objects.
———–
For example,
>>>p = Card.objects.create(name=’Hot Pot’, city=’San Diego’)
>>>card_list = Card.objects.all()
>>>card_list
[<Card: Card object>]
without __unicode__, you will see just “Card object”
if you define __unicode__
def __unicode__(self):
return self.name
then, you will see
[<Card: Hot Pot>]

Posted by weboom at 5:22 am Tagged with: , ,
Aug 022009

Python list
isĀ  a container that holds a number of other objects, in a given order.
L=[1, 2, 3, 4]

Posted by weboom at 5:02 am Tagged with: ,
Switch to our mobile site
Page 1 of 212»Top Footer