In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Python full stack file function and what are the parameters of the function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python full stack file functions and what are the parameters of the function" bar!
1. File related function # refresh buffer "" # refresh buffer flush # automatically flush the buffer when the file is closed # automatically flush the buffer when the whole program is finished # automatically flush the buffer when the buffer is full # manually refresh the buffer "fp = open (" ceshi1.txt ", mode=" a ") Encoding= "utf-8") fp.write ("abc") # manually flush the buffer and write the contents directly to the file fp.flush () while True: passfp.close () "" # File related functions "fp" the object itself is an iterator, and the contents of the file can be traversed line by line "fp = open (" ceshi1.txt ", mode=" r "). Encoding= "utf-8") # readable () function: judge whether a file object is readable print (fp.readable ()) # writable () function: determine whether a file object is writable print (fp.writable ()) # traversal fp file object for i in fp: print (I) "" # 1.readline () function: read a line of file contents''with open ("ceshi1.txt", mode= "r") Encoding= "utf-8") as fp: res = fp.readline () print (res) # (1) read it all at once with open ("ceshi1.txt", mode= "r") Encoding= "utf-8") as fp: # first read a line res = fp.readline () # to determine whether it is empty, not empty in the loop while res: print (res) # read a line and put it in the loop. Res = fp.readline () # (2) Note: readline (number of characters read) print ("") with open ("ceshi1.txt", mode= "r", encoding= "utf-8") as fp: number of characters read > actual number of characters on the current line = > number of characters read according to the current line
< 实际当前行字符数量的时候 =>Read "" res = fp.readline (300) print (res)''print ("") # 2.readlines () function: read the contents of the file into the list according to the newline lst_new = [] with open ("ceshi1.txt", mode= "r +") Encoding= "utf-8") as fp: lst = fp.readlines () for i in lst: lst_new.append (i.strip ()) print (lst_new) # 3.writelines () function: write repeatability data of string content to file parameters: iterative data of string type lst = ['bright moonlight in front of bed', 'like cold frost on the ground' With open ("ceshi2.txt", mode= "w+", encoding= "utf-8") as fp: fp.writelines (lst) # achieve effect: add a line break effect, and insert a sentence: Wang Wen is really handsome, insert the content in front of bowing his head and thinking about home lst_new = [] # insert the content into the original list lst.insert (- 1) ("Wang Wen is really handsome") # Loop the original list, put each element together\ ninto the new list for i in lst: lst_new.append (I + "\ n") print (lst_new) # insert each line in the new list into the file with open ("ceshi2.txt", mode= "w +", encoding= "utf-8") as fp: fp.writelines (lst_new) # Note, the content must be a string Cannot be integer "lst = [1Jing 2jue 3] with open (" ceshi2.txt ", mode=" w+ ", encoding=" utf-8 ") as fp: fp.writelines (lst)"# 4.truncate () function: extract the string to be intercepted, then empty the content and rewrite the extracted string into the file (bytes) with open (" ceshi2.txt ", mode=" r + ") Encoding= "utf-8") as fp: fp.truncate (3) "" seek (byte) truncate (byte) read (character / byte) readline (character / byte) "" 2. Function _ function parameter 2.1 function # function "concept: function (wrapping a part of the code to achieve a certain function to achieve a certain purpose) characteristics: it can be called repeatedly to improve the reusability of the code and improve the development efficiency. Easy maintenance and management. Function basic format "" # define a function def function name (): code1 code # call function name () "" # define function def func (): print ("I am a function.") # call function func () # 2. The function is named "" alphanumeric underscore, the first character cannot be strictly case-sensitive, and it is meaningful to use keyword functions. And cannot use Chinese, oh hump nomenclature: (1) Big hump nomenclature: the first character of each word should be capitalized (class naming) mycar = > MyCar (2) small hump nomenclature: except for the first word in lowercase. The first characters of the remaining words are capitalized (function or variable) mycar = > myCar_ nomenclature: different words can be concatenated with _ mycar = > my_car symmetric_differencesymmetricDifference SymmetricDifference "" # function definition def cfb_99 (): for i in range (1d10): for j in range (1) Item1): print ("{: d} * {: d} = {: 2d}" .format End= "") print () # call function for i in range (5): parameter of cfb_99 () 2.2.The parameter of function # Parameter of function "" parameter: type of value parameter needed for function operation: (1) formal parameter: formal parameter, at the definition of function (2) argument: actual parameter The types of formal parameters in the call of the function: 1. Common formal parameter (position formal parameter) 2. Default parameter 3 general collection parameter 4. Name key shape Ref. 5. Keyword collects the types of formal parameters: 1. Common actual reference 2. Keyword argument principle: one-to-one correspondence between formal parameter and actual parameter "# 1. General formal parameter (position parameter) # define function" hang,lie common formal parameter, at the function definition "def small_star (hang,lie): I = 0 while I
< hang: j = 0 while j < lie: print("*",end="") j +=1 print() i += 1# 调用函数"""10,10普通实参,在函数的调用处"""small_star(10,10)small_star(2,3)# 2.默认形参 """hang,lie默认形参,在函数定义处""""""如果给予实参,那么使用实参如果没有给予实参,那么使用参数身上的默认值"""def small_star(hang=10,lie=10): i = 0 while i < hang: j = 0 while j < lie: print("*",end="") j +=1 print() i += 1small_star(4,8)small_star(8)small_star()# 3.普通形参 + 默认形参"""普通形参必须写在默认形参的前面不能调换位置"""def small_star(hang,lie=10): i = 0 while i < hang: j = 0 while j < lie: print("*",end="") j +=1 print() i += 1small_star(5,7)# small_star(5)# small_star() error# 4.关键字实参print("")"""1.如果都是关键字实参,可以任意调整实参的顺序2.普通实参必须写在关键字实参的前面"""def small_star(hang,a,b,c,lie=10): i = 0 while i < hang: j = 0 while j < lie: print("*",end="") j +=1 print() i += 1# hang a ... lie 具体指定参数的值叫做关键字实参,在函数的调用处;# small_star(hang=3,a=4,b=5,c=6,lie=7)# small_star(b=5,c=6,lie=7,a=4,hang=3)small_star(3,4,b=5,c=6,lie=7)small_star(3,4,b=5,lie=7,c=6)# small_star(b=5,c=6,lie=7,3,4) error3. 收集参数# ### 收集参数"""(1) 普通收集形参: 专门用来收集那些多余的没人要的普通实参 收集之后,会把多余实参打包成一个元组 参数头上1个星星 def func(*args): pass args =>Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.