Python strings, list lists, dictionaries, files related operations

Source: Internet
Author: User

1, the Boolean type string common operation method. Count (). Find (). Join (). Encode (). Decode () #这个是bytes类型才有的. EndsWith ('. txt ') #字符串是否以xx结尾startswith (' A ') #字符串是否以xx开头. IsDigit (). Strip (). Replac (' xx ', '). Split (', ') #以xx来分割字符串, returns a list. Splitlines () #以换行符来分割字符串, Return is a list 2, lists of additions and deletions to check the subscript value, the subscript is a 0-based slice: Gu Tou Regardless of the tail slice colon before the value, omitted not to write, represents the previous value of the slice colon after the value, omitted not to write, the representative take all the values of the following only one colon, take the entire list, It can be used to copy a new list step is not written by default, is 1, write on the step, write a few elements to take a step to a negative number of times, from the end of the list to take a value, is a positive number, from the beginning of the list to take a value. The operation of the slice subscript also applies to string increment # creation, add value to list szz = [' bjz ', ' LW ', ' lhl ', ' jjx ', ' CSS '] #创建一个列表szz. Append (' Ymn ') #从末尾添加值szz. Insert (0, ' Zyj ') #在指定的位置添加值print (SZZ) Check: Subscript value, slice value Delete: # del szz[0] #删除指定下标的值 # Szz.pop (2) # Szz.pop () #默认从列表的末尾开始删除, can also be passed to the specified subscript for deletion # Szz.remove (' JJX ') #删除指定的值 instead of subscript szz.clear () #清空列表改szz [0]= ' on-the-walk ' on the ' J-P ' Print (' Modified list: ', SZZ) List of built-in Operation methods # Nums.sort (reverse =true) #排序, default in ascending order, specify reverse=true# print (Szz.index (' ly ') #获取指定元素的下标 # print (' Szz.count (' Zl ')) #获取指定元素的次数 # If you want to follow the descending shot Szz.reverse () #反转字符串 # szz.extend (SZZ1) #扩展字符串元组就是一个不可变的列表, it has only two methods mysql_config= (' 127.0.0.1 ', 3306, ' root ', ' 123456 ') Mysql_config.count (3306) #查询出现的次数Mysql_config.index (' root ') #获取指定元素的下标3, dictionary additions and deletions to the dictionary built-in operation Method 4, the file read and write open (encoding= ' utf-8 ') Python2, Python3file () # PYTHON2R Read Only # W write #a append to write, write, read, append read u# all the files inside the newline character, are changed to \nb# binary # three mode. Read () #读取文件的所有内容, return a string. ReadLines () # Reads all the contents of the file, returns a list, and the contents of each row is an element of the list. ReadLine () #读一行for i in F:print (i). Write () #写一个list. Seek ( ) #移动文件的指针到xx位置. Tell () #查看文件当前的指针在哪里. Truncate () #清空文件. Close () #关闭文件. Flush () #立即把内存里面的数据写到文件里面with open (' xx ') as FW:FW. () If you want to modify the original file, you must create a new file, the source of the modified value of the file to write to the new file inside Os.remove (' a.txt ') os.rename (' A.txt.bak ', ' a.txt ')

Python strings, list lists, dictionaries, files, and related actions

Related Article

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.