-----------------
>>> ord(b'A')
65
>>> ord(b'!')
33
>>> list(b'Example')
[69, 120, 97, 109, 112, 108, 101]
>>> chr(65)
'A'
>>> chr(33)
'!'
>>>'This is an example, with punctuation and UPPERCASE.'.encode('ascii')
b'This is an example, with punctuation and UPPERCASE.'
>>> ascii = 'This is a test'.encode('ascii')
>>> ascii
b'This is a test'
>>> ascii.decode('utf-8')
{0}, {1}
>>> import os.path
>>> '{0:>20}{1}'.format(*os.path.splitext('contents.txt'))
' contents.txt'
>>> for filename in ['contents.txt', 'chapter.txt', 'index.txt']:
... print('{0:<10}{1}'.format(*os.path.splitext(filename)))
contents .txt
chapter .txt
index .txt
>>> heading('Standard Format Specification')
'=====Standard Format Specification======'
>>> heading('This is a longer heading, beyond 40 characters')
'=This is a longer heading, beyond 40 characters='
>>> heading('Standard Format Specification', padding='-', width
'---------------Standard Format Specification----------------'
print string + str(123)
days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
for i, d in enumerate(days):
print i, d
0 Mon
1 Tue
2 Wed
3 Thu
4 Fri
5 Sat
6 Sun
-----------------
String and Parsing
http://www.tutorialspoint.com/python/string_find.htm
str1 = "this is string example....wow!!!"; str2 = "exam"; print str(len(str1))+ " " +str(len(str2)) print str1.find(str2) print str1.find(str2, 10) print str1.find(str2, 40) print str1.find("this", 0, len(str1)) str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ) print str.split(' ', 1 ) str1.capitalize() str1.lower() str1.upper()
-----------------
File
f = open("textfile.txt") # by default, read only f = open("textfile.txt","r") f = open("textfile.txt","w+") f = open("textfile.txt","a+") if f.mode == 'r': # check to make sure that the file was opened txt = f.read() # read entire content fl = f.readlines() # readlines reads the individual lines into a list for x in fl: print x # write some lines of data to the file for i in range(10): f.write("This is line %d\r\n" % (i+1))
-----------------
File Path
import os from os import path import datetime from datetime import date, time, timedelta import time def main(): # Print the name of the OS print os.name # Check for item existence and type print "Item exists: " + str(path.exists("textfile.txt")) print "Item is a file: " + str(path.isfile("textfile.txt")) print "Item is a directory: " + str(path.isdir("textfile.txt")) # Work with file paths print "Item's path: " + str(path.realpath("textfile.txt")) print "Item's path and name: " + str(path.split(path.realpath("textfile.txt"))) # Get the modification time t = time.ctime(path.getmtime("textfile.txt")) print t print datetime.datetime.fromtimestamp(path.getmtime("textfile.txt")) # Calculate how long ago the item was modified td= datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime("textfile.txt")) print "It has been " + str(td) + "The file was modified" print "Or, " + str(td.total_seconds()) + " seconds" if __name__ == "__main__": main()
-----------------
Regular Expression
import time, os, re # display time corresponding to your location print(time.ctime()) f=open("sink_info.txt") txt = f.read() #AU_LINUX_ANDROID_LA.BF64.1.2.2.05.01.01.110.103 result = re.search("AU_LINUX_ANDROID_LA.\w+.\d.\d.\d.\d{2}.\d{2}.\d{2}.\d{3}.\d{3}", txt) #result = re.search("AU_LINUX_ANDROID_LA.(HB|BF64).[0-9].[0-9].[0-9].[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{3}.[0-9]{3}", txt) print result if result: tmp1=result.group(0) #tmp2=result.group(1) print tmp1 #print tmp2 #RUN SINK command with this AU
'Programming' 카테고리의 다른 글
Python - JSON을 이용한 지진검색 (0) | 2015.10.01 |
---|---|
VM Virtualbox에 Ubuntu설치후 (0) | 2015.10.01 |
Python - Stock Price Quote (0) | 2015.09.30 |
VIM resource file (0) | 2015.09.30 |
Python - Date, Time (0) | 2015.09.29 |