learning python string dictionary --- common method ---- ---- tuple basic file operations

Dictionary # 
# by key: value data storage mode
# key value through the high efficiency
info [ 'name']
# modify the original value
info.setdefault ( 'name', 'LDH')
# does not change the original data
# modify
info [ 'name'] = 'value'

# remove
info.pop ( 'Key')
del info [ 'Key']
info.popitem () to delete a random data #

# check
info.get ( 'Key')
info .get ( 'key', 'default')
# key if the key corresponding to the presence of the return value
# default value if the key does not exist is returned
info [ 'key']

info.keys ()
# Get all dictionary key
info .values ()
# get all values dictionary
info.clear ()
info.update (info1) # dictionary combined
# info1 be added to the info
key in info
# determines whether the info key is present in the

conventional method # string
str.Strip ()
# remove both sides of the spaces and line breaks string
# can not be removed in the middle of the string
str.rstrip ()
# remove line breaks and spaces to the right of the string
str.lstrip ()
# remove the string to the left of spaces and line breaks
str.replace ( "", "")
# replacement string content
str.index ( 'a')
# Find a subscript
str.count ( 'KA')
# number of characters that appear
str.find ( 'a')
# Find subscript
str.startswith ( '')
# xxx beginning has been to determine whether
str. endsWith ( '')
# xxx has been determined whether the end
str.upper ()
# becomes all the uppercase letters
str.lower ()
# all lowercase letters are changed
str.isdigit ()
# determines whether an integer
str.capitalize ( )
# capitalized
str.center (50, '=')
# total length of 50
# if insufficient length to both sides of the padded number =
str.zfill (2)
# 0 up in front of the character
str.isupper ()
# determines whether all capital letters
str.islower ()
# determine whether all lowercase
str.isialpha ()
# if not letters or characters
# full return to true
str.isalnum ()
# only numbers or letters or characters will not return to true
# else return false
str.format ()
# string formatting

str = 'today is {}, {} welcome log '
str.fromat (' a ',' B ')

str.split (' strings separated ')
# delimited string
' specified string '.join (list)
# inside of the list each element is connected by a specified string up
s4 = 'insert into stu (id , username, passwd, phone, addr, qq, email, score) value ( "{id}", "{username}", "{password } "," {} Phone "," addr {} "," {} QQ "," In Email {} "," {} Score ") '
s4.format (username =' AAA ',. 1 ID = ????? ·····)
s4.format_map ({ 'username': 'ddd', 'ID':········· 234})

# tuple
# tuple is a list, and can not be modified
# read only
T = (. 1, 2,. 3,. 4,. 5)
T [0]
# value
t. count ( "xxx")
# Get the number xxx appearing in the tuple
t.index ( "value")
# Get subscript

- -
Import the pprint

# complete print format data type

# file operation
# 1, the document reading
Open ()
# open file three modes
# read
f = open (r 'file path', encoding = 'GBK')
Result reached, f.read = ()
Print (Result)
f.close ()
# can only be read but not written

# written
f = open ( 'file path ',' w ', encoding =' GBK ')
f.write (' 123456 ')
f.close ()
# can only write can not read, the contents will be covered before each write, the file does not exist will be created

# append
f = open ( 'file path', 'A', encoding = 'GBK')
f.write ( '7890')
f.close ()
# can only write can not read, appended to the original file contents later, the file will be created does not exist

# reading write mode
# r + r is about as long as the open file does not exist are given
f = open ( 'C:\\Users\\LDH\\Desktop\\123.txt','r+',encoding='gbk')
f.write ( 'ABC')
Result reached, f.read = ()
Print (Result)
f.close ()

# write-read mode
# w + w is about as long, will overwrite the contents of the source file
f = open ( 'C: the Users of LDH \\ Desktop \\ \\ \\ 123.txt ',' W + ', encoding =' GBK ')
f.write (' RTY ')
Result reached, f.read = ()
Print (Result)
f.close ()

# a + created file does not exist, the contents of the file append

F = Open ( 'C: \\ the Users of LDH \\ \\ \\ 123.txt Desktop', '+ A', encoding = 'GBK')
F. Write ( 'UIO')
Result reached, f.read = ()
Print (Result)
f.close ()

# a + time required to read the file pointer to the front
f = open ( 'C: \\ Users \\ LDH \ \ \\ 123.txt Desktop ',' + a ', encoding =' GBK ')
f.seek (0)
Print (f.readlines ())
# returns a list of each row as an element
f.seek (0) # move the file pointer pointing to the beginning of the line
Print (reached, f.read ())
f.close ()

f.writelines (list)
# automatic cycle, each element in the list are written to the file
L = [ '. 1 \ n-', '2 \ n-']
F = Open ( 'C: \\ \\ the Users of LDH \ \ \\ 123.txt Desktop ',' + A ', encoding =' GBK ')
f.writelines (L)
f.seek (0)
Print (reached, f.read ())
f.close ()

file modify
# efficient processing of documents
# 1, a first method, first find the content to be modified and replaced, empty file, the contents of re-rewriting through replacement file
f = open ( 'liudonghai.txt', 'a +', encoding = 'utf- . 8 ')
f.seek (0)
Result reached, f.read = ()
RESULT1 = result.replace (' wangyanquan ',' 123 ')
f.seek (0)
f.truncate () Clear file #
f.write (result1)
f.close ()
# 2, big files processed line
Import OS

F = Open ( 'liudonghai.txt', '+ A', encoding = 'UTF-. 8')
F1 = Open ( 'liudonghai2.txt', 'w', encoding='utf-8')
f.seek(0)
Line F in for:
Line = line.upper ()
f1.write (Line)
f.close ()
f1.close ()
The os.remove ( 'liudonghai.txt')
os.rename ( 'liudonghai2.txt', 'liudonghai .txt ')

# use with the open file does not need to use complete with automatically close off
with open ( "liudonghai.txt") as f, open (' file path ') AS F2:
F = reached, f.read ()
F2. write ( 'xx')

Guess you like

Origin www.cnblogs.com/huaixiaohai/p/10939692.html