Přejít na menu

Python

Správa článků

Vyhledávání Vyhledávání
27.10.2012 18:58
,
Počet přečtení: 665

Můj tahák a kuchařka na Python.

Dicts

Dict comprehension

d = dict((key, value) for (key, value) in iterable)

From Python 2.7 and 3 onwards, you can just use the dict comprehension syntax directly:

d = {key: value for (key, value) in iterable}

Výstup

Výstup na stdout bez odřádkování

import sys
sys.stdout.write(.)

Výstup na stderr

import sys
sys.stderr.write("output")

Another version for Python 3 of improted from __future__:

from __future__ import print_function
print("WARNING: You were warned!", file=sys.stderr)

Filesystém

Zápis do souboru

try:
f = open("/path/to/file.txt", "w")
f.write("some text")
f.close()
except IOError, e:
print("Can't write to the file!")
#endtry

Iterating over file lines without loading the whole file into the memory

import fileinput
for line in fileinput.input(['myfile']):
    do_something(line)

Read file line by line into array

with open(fname) as f:
    content = f.readlines()

Copy the file

from shutils import copyfile
copyfile("src/file", "new/file")

Generators & list comprehension

# Generator expression
(x*2 for x in range(256))

# List comprehension
[x*2 for x in range(256)]

Generators create items dynamically, so can be used for infinite sequences. They are slower, but don't occupy memory.

With statement

Explanation: http://effbot.org/zone/python-with-statement.htm

Controlled execution: setup and teardown with the object, for example the file handle.

Regexps

import re

Matching

if m.match(r"I love (\w+)"): 
        print "He loves %s" % m.group(1)

Substitution

result = re.sub(pattern, replacement, subject[, limit])

result = re.sub("bad guy", "good guy", my_string)

Attention for backslashes: Python interprets character with backslash as single character, ex. "\1" is "\x01". Use raw strings instead: r"\1"

Dump objektu

my_object.__dict__

More...

Mutable default function parameter

def my_function(a=[])
    a.append("b")
    print a

>>> my_function()
["b"]
>>>my_function()
["b", "b"]

(Source)

Parsování Wikipedie

Je potřeba změnit useragenta:

import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes')
page = infile.read()

utf8

http://www.evanjones.ca/python-utf8.html

Nastavení defaultního kódování:

# sys.setdefaultencoding() does not exist, here!
import sys
reload(sys) # Reload does the trick!
sys.setdefaultencoding('UTF8')

Vytvořil 27. října 2012 v 18:59:31 mira. Upravováno 26x, naposledy 22. června 2015 v 18:39:31, mira


Diskuze ke článku

Vložení nového komentáře
*
*
*