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()

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

Use readlines()

For example,

arg = "filename"

f = open(arg, 'r')

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

>>>import os

>>>os.chdir(‘..’)

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

Then,

import datetime

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': 4098}
>>> tel.keys()
['guido', 'irv', 'jack']
>>> 'guido' in tel
True

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.

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”)

__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>]

Python list

isĀ  a container that holds a number of other objects, in a given order.

L=[1, 2, 3, 4]

© 2011 Web Oom Suffusion theme by Sayontan Sinha
Switch to our mobile site
Page 1 of 212»Top Footer