My method of Python

Source: Internet
Author: User

defDel_blank_line (filename):#clear File blank line    """Clear blank line of file:p Aram FileName: file name: Return:true succeeded; False failed"""    Try: With open (filename,"r+", encoding="Utf-8") as Infp:lines= Infp.readlines ()#read the contents of the source file and save it in lineswith open (filename,"w+", encoding="Utf-8") as OUTFP: forLiinchlines:ifLi.split ():#determine if a blank lineOutfp.writelines (LI)#overwrite the source file after the action is written back    exceptIOError:Print("%s file does not exist or has no Operation permission"%filename)returnFalseElse:        returnTrue
clear File blank line
defGet_ip_list (filename, repeat=False):"""obtain a valid IP address in the file:p Aram FileName: file name:p Aram Repeat: Remove duplicate line, true remove, false not remove: return: Returns IP address sequence"""    ImportReTry: With open (filename,"R", encoding="Utf-8") as File1:line=file1.read () pattern=Re.compile (R"(?:\ B (?: \ D{1,2}|1\D{2}|2[0-4]\D|25[0-5]) \b\.) {3} (?:\ D{1,2}|1\D{2}|2[0-4]\D|25[0-5]) \b") List_ip=Pattern.findall (line)ifLen (list_ip) = =0:returnlist_ipexceptIOError:Print("%s file does not exist or has no Operation permission"%filename)returnFalseElse:        ifrepeat = =True:returnSet (LIST_IP)elifrepeat = =False:returnList_ip
get a legitimate IP address in a file
defGet_check_code (n = 6):    """gets a random n-bit verification code consisting of uppercase and lowercase letters, numbers:p Aram Num: Verify the number of code bits, the default is 6:return: return n-bit verification code"""    ImportRandom Check_code=str () code=str () forIinchrange (n): Ret= Random.randint (0, 9)        ifret = = 0orret = = 1orRET = = 4orRET = = 7: Code=STR (ret)elifRET = = 2orRET = = 5orRET = = 8: Code= Chr (Random.randint (65, 90))        elifRET = = 3orRET = = 6orRET = = 9: Code= Chr (Random.randint (97, 122)) Check_code= Check_code +CodereturnCheck_code
Get N-bit random verification code

My method of Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.