码迷,mamicode.com
首页 > 编程语言 > 详细

python之路2

时间:2018-10-09 20:02:42      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:pca   空格   utf8   count   title   wap   api   dex   values   

Python之路02 

字符                     

if age.isdigit(): #字符串age是数字”12”
   age = int(age)

else:

exit()        #退出程序

exit(“现在我退出了”)

截取

print(‘helloworld‘[2:])

属于 

print(‘el‘ in ‘hello‘) 

格式化 

%s string

%d  整数

%f 浮点数xx.xxx xxx


msg = ‘‘‘name =%s,age =%s‘‘‘%(name,age)
print(msg)

连接

d2=‘--‘.join([a,b])

print(d2)  # 123-- abc

方法############

print( st.count(内容) )      #  统计元素个数

print( st.endswith(‘tty3‘) )  #  判断是否以某个内容结尾

print( st.startswith(‘he‘) )  #  判断是否以某个内容开头

print(st.find(‘t‘))        #  查找到第一个元素,并将索引值返回

print(‘My title title‘.rfind(‘t‘))

print(‘12699‘.isdigit())              #是否是‘123456’字符串整型数字

print(‘\tMy tLtle\n‘.strip())              #去换行符,空格

print(‘\tMy tLtle\n‘.lstrip())

print(‘\tMy tLtle\n‘.rstrip())

print(‘My title title‘.replace(‘itle‘,‘lesson‘,1))  #把itle替换为lesson ,替换几次

print(‘My title title‘.split(‘i‘,1))  #仅以第一个i为分隔符

 

print( st.capitalize() )      #  首字母大写

print(‘Abc‘.islower())

print(‘ABC‘.isupper())

print(‘  e‘.isspace())

print(‘ tLtle‘.lower())

print(‘tLtle‘.upper())

print(‘My tLtle‘.swapcase())  #大写变小写,小写变大写

print( st.center(50,‘#‘) )    #  居中      长度50,#填充

print(‘My tLtle‘.ljust(50,‘*‘)) #左

print(‘My tLtle‘.rjust(50,‘*‘))            #右

print(‘My tLtle‘.title())              #每个单词首字母大写

-------------------------------------------------------------------------------

摘一些重要的字符串方法

print(st.count(‘l‘))

print(st.center(50,‘#‘))   #  居中

print(st.startswith(‘he‘)) #  判断是否以某个内容开头

print(st.find(‘t‘))

print(st.format(name=‘alex‘,age=37))  # 格式化输出的另一种方式  

print(‘My tLtle‘.lower())

print(‘My tLtle‘.upper())

print(‘\tMy tLtle\n‘.strip())

print(‘My title title‘.replace(‘itle‘,‘lesson‘,1))

print(‘My title title‘.split(‘i‘,1))

列表

a = [[3,5],
     [1,2],
     [5,8],]

print(a[2][0])

a.append()

a.insert(下标, 内容)  #在下标之前插入

a.extend(a)

a.remove(内容)  
a.pop(下标)     #pop()删除尾元素      

a.clear()    #数组清空

del a       #数组置null       

del a[index]

a[下标] = "新的值"
a[ start : end ] = ["内容","内容"]

[ : -1 : 步长]
print(a) #[1, 1, 8]
print(a[0:]) #[1, 1, 8]  打印数组全部

排序

a.sort ()
sorted(a)  #排序
 a.reverse()

其他

a.count[内容] 元素出现次数

a.index[内容] 元素出现索引

元组-只读()

字典

不可变类型:整型,字符,元组       键--不可变类型

可变类型:列表,字典

 

字典两大特点:数据无序,键唯一

创建

dic={‘hobby‘:{‘girl_name‘:铁锤,‘age‘:45}, }    值可为字典

dic[‘name‘]=‘alex‘

dic.update(dic2)

dic.pop(‘age‘)

dic.clear()

del dic

del dic[‘name‘]

print( list(dic.keys())   )   打印出所有的键 , 一个列表

print( list(dic.values()) ) 所有值

print( list(dic.items())  )  所有键值对

其他

复制

copy_dic = dic.copy()

嵌套

av = {  "欧美":{

         "www.youporn.com": ["很多免费的,世界最大的","质量一般"],

         "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]

     },

}

排序
sorted(dic)

字典的遍历  

for i in dic:

    print(i,dic[i])

Python之路03

文件

能调用方法的一定是对象

格式

data = open(路径,模式,encoding=’utf-8’)     

模式

r(只可读)  w(只可写-清空写) a(追加,文末写)

r+  开头读,末尾写

w+  w正常,+先清空,再写读

a+  末尾读

read,write

f.read()   打印所有

f.read(2)  打印2个汉字/字母

print(data.readline().strip())

f.readlines() 每行内容成为一个列表元素

 

对于大文件的read

number=0

 for i in f:                 #这是for内部将f对象做成一个迭代器,用一行取一行。

     number+=1

     if number == 6:

         i = ‘‘.join([i.strip(), ‘abc123‘])  # 取代万恶的+

     print(i.strip())

f.tell()

print(f.tell())  #默认Unicode编码,中文3,字母1取出光标位置

f.seek(0)

f.seek(0)   移动光标到指定的位置

flush()

将数据从缓存转移到磁盘上去

import sys,time

for i in range(30):

sys.stdout.write("*")

sys.stdout.flush()

    time.sleep(0.1)

 

print的flush

import time

for i in range(30):

    print(‘*‘,end=‘‘,flush=True)     #end表示不换行

    time.sleep(0.1)

在磁盘修改文件

f_read=open(‘小重山‘,‘r‘,encoding=‘utf8‘)

f_write = open(‘小重山2‘,‘w‘,encoding=‘utf8‘)

number=0

for line in f_read:

     number+=1

     if number==5:

         line=‘‘.join([line.strip(),‘alex\n‘])

     f_write.write(line)

f_read.close()

f_write.close()

字典->字符串

b = {‘beijing‘:{‘1‘:111}}

a=str( b ) 

字典->字符串

a=eval(a)         

 

with

 with open(‘log‘, ‘r‘) as f:

     f.readline()

     f.read()

不需要close

with 同时管理多个文件对象

with  open(‘log1‘,‘r‘)  as  f_read  ,  open(‘log2‘,‘w‘)  as  f_write:

     for line in f_read:

        f_write.write(line)

truncate()删数据(不能在r模式下)

在w模式下:先清空,再写,再截断

a模式下:直接将指定位置后的内容截断

f.truncate()      #删除5字符后面的内容

f.write(‘hello world‘)

f.truncate(5)

f.close()

 

python之路2

标签:pca   空格   utf8   count   title   wap   api   dex   values   

原文地址:https://www.cnblogs.com/5014sy/p/9762201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!