Posted in Information Technology, Software Engineering

Python Syntax Examples


Data Types

Dumping complex structures

import json
print json.dumps(something)

Arrays (Lists)

Print array of strings:

print "\n".join(arr)

Iterating by index

for i in range(len(arr)):
  print arr[i]

Standard Tasks

Read from Pipe

Line by line

proc = subprocess.Popen(['/bin/ls', 'something'],stdout=subprocess.PIPE)
while True:
  line = proc.stdout.readline()
  if line != '':

Entire output at once

echo subprocess.check_output(['/bin/ls', 'something])

Parse JSON

import json
data = json.loads(json_string)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s