python三 文件与string与编码

一、文件处理

 ①打开文件  open(file, mode='r', buffering=-1, encoding=None)

  

 t和+:默认模式为'r'读取文本; 'w+'、'w+b'打开文件并清空,不存在则创建,a+也是不存在则创建; 'r+' 与 'r+b' 仅打开文件但不清空。

 mode: 文本模式时endoding=平台所使用的编码,或用locale.getpreferredencoding(False) 来获取本地编码。

 buffering:缓冲区字节数,b模式下才可以是0; pycharm中file->settings->Editor->File Encodings设置编码方式。

 文件关闭模式:    

try:                          官方推荐:
f
= open('test.txt', 'r')            with open('test.txt','r') as f: # do_something()                    # do_something() finally:                        同时打开多个文件: f.close()                      with open('a.txt','r') as read_f, open('b.txt','w') as write_f: 

 文件test.txt不存在时,r或r+模式下会抛出FileNotFoundError,其他模式下会创建,其中w和w+模式下会清空已存在文件里的内容。

文件写入读出

 1) f.write(str)        2) f.writelines(str)  两者功能一致,str是什么就写入什么。

 3)写入字符串list=['str1','str2'],f.writelines("%s\n" % x for x in list)

 读取文件:

 1)str=f.read()读取整个文本。

 2)list=f.readlines()list=list(f) 读取整个文本,字符串的list。

 3)str=f.readline()读取文本一行,文件尾时str='',可if not str: 或if str==''来判断。

文件位置

 1)f.seek(int1,int2):移动int1个字节,int2取0、1或2,代表绝对、相对于当前、文尾三种情况。

 2)position = f.tell()

print() 输出  print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) eg: 

 二、string常见操作

①状态判断 "string".islower()                               

  • islower()、isupper()、isdigit()  判断是否全为小写、大写、数字
  • isalpha()        是否为纯字母
  • isalnum()          是否为字母和数字 没有以外的字符
  • istitle()             每个单词是否大写开头
  • isspace()         是否只有空白字符(空格回车制表符)
  • startswith(str)     是否以某字符串开头
  • endswith(str)    

②字符转换

  • lower()、upper()                  逐字符转换为小写、大写
  • swapcase()                        字符串中大写转小写,小写转大写
  • casefold()                        字符串中所有大写字母转小写
  • title()                         字符串每个单词首字母大写,其他小写
  • capitalize()                      字符串首字母大写,其他小写

③修理和拆分  

  • strip ([substr])                       去掉字符串中的substr, 默认空白字符
  • lstrip (substr)                        只去左边
  • str1+str2、str1+=str2
  • split(substr)                         按substr拆分为list
  • splitlines ()                         按换行符拆分为list
  • partition(substr)                       从左侧按substr拆分为三部分, "xuchuan".partition("o") -> ("","","xuchuan")

④查询

eg: "xuchuan".find("u", 0, len("xuchuan"))==4

  • find(substr[, begin[, end]])                 在字符的[begin, end)段从左开始,查询最后一个匹配substr的下标
  • rfind(substr, begin, end)                     从右开始查, 未找到返回-1
  • index(substr, begin, end)                     查询第一个匹配substr的下标
  • count(substr)                            能匹配的substr的个数

⑤替换

  • replace(oldnew[, count])                   只替换前count次出现的old

三、编码和解码

  bytes数据类型是“不可变的二进制 字节数据”,能使用str类型大部分方法。

定义:vb = b'abXY'    # bytes类型

    vs = u'万水千山'   # Unicode字符串

编码:str -> bytes,  解码:bytes -> str

 

最后、内置函数list常用方法  

string参考:

 https://www.cnblogs.com/fandx/p/9709192.html

 https://docs.python.org/zh-cn/3/library/stdtypes.html 

 https://docs.python.org/zh-cn/3/library/functions.html 内置函数文档

参考:

1.https://docs.python.org/zh-cn/3/library/functions.html#open input 内置函数

2.https://www.cnblogs.com/livingintruth/p/3264001.html

3.https://www.cnblogs.com/AkumaIII/p/8041136.html

2020-03-03 19:01:39

posted @ 2020-03-03 19:02  shines87  阅读(395)  评论(0)    收藏  举报