返回列表 发帖

字符串的截取

操作系统的版本信息:
[root@centos6 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@centos6 ~]#
[root@centos6 ~]# uname -r
2.6.32-696.el6.x86_64

Python的版本信息:
[root@centos6 ~]# python3 --version
Python 3.6.8



######

例子一:

[root@centos6 ~]# cat xx.py
#coding=utf-8

Sting_1 = "阿里云服务器('47.75.39.177'),22"
print(Sting_1)

print("-" * 10)

a = Sting_1[:-5] #去掉最后5个字符
print(a)

b = a[2:] #去掉前面2个字符
print(b)

c = b[6:] #截取从第7位开始到最后的字符
print(c)

d = c[2:9] #截取3到9的字符
print(d)

e = d[0:4] #输出前面4个字符
print(e)

f = e[-3:] #输出最后3个字符
print(f)


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
阿里云服务器('47.75.39.177'),22
----------
阿里云服务器('47.75.39.177
云服务器('47.75.39.177
47.75.39.177
.75.39.
.75.
75.
[root@centos6 ~]#



######

例子二:

[root@centos6 ~]# cat xx.py
#coding=utf-8

Sting_1 = "----==zhuohua----===="
print(Sting_1)

a = Sting_1.rstrip('=') #单单去除右边的"="
print(a)

b = a.lstrip('=-') #单单去除左边的"="、"-"
print(b)

print("#" * 20)

Sting_2 = "----==+Python+---===="
print(Sting_2)
c = Sting_2.strip('-=') #去除左边和右边的"="、"-"
print(c)

d = c.rstrip('+n') #单单去除右边的"+"、"n";区分英文字母大小写的
print(d)

e = d.lstrip('+P') #单单去除右边的"+"、"P";区分英文字母大小写的
print(e)


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
----==zhuohua----====
----==zhuohua----
zhuohua----
####################
----==+Python+---====
+Python+
+Pytho
ytho
[root@centos6 ~]#



######

例子三:

[root@centos6 ~]# cat xx.py
#coding=utf-8

Sting_1 = '\n  Welcome to zhuohua. \n ' # \n 换行符
print(Sting_1)

print("-" * 10)

a = Sting_1.strip() #去除左边和右边的空格、换行符
print(a)

b = Sting_1.lstrip() #单单去除左边的空格、换行符
print(b)

c = Sting_1.rstrip() #单单去除右边的空格、换行符
print(c)

print("-" * 20)


脚本运行的结果:
[root@centos6 ~]# python3 xx.py

  Welcome to zhuohua.

----------
Welcome to zhuohua.
Welcome to zhuohua.


  Welcome to zhuohua.
--------------------
[root@centos6 ~]#



######

例子四:

[root@centos6 ~]# cat xx.py
#coding=utf-8

Sting_1 = 'Welcome to zhuohua.'

print(Sting_1.center(30)) #居中显示;"30"是重新自定义字符串的总长度
print(Sting_1.ljust(30))  #靠左显示
print(Sting_1.rjust(30))  #靠右显示


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
     Welcome to zhuohua.      
Welcome to zhuohua.           
           Welcome to zhuohua.
[root@centos6 ~]#  





############
############

测试所使用的文件:
[root@centos6 ~]# cat /root/1.txt
文件系统             容量  已用  可用 已用% 挂载点
devtmpfs             883M     0  883M    0% /dev
tmpfs                901M     0  901M    0% /dev/shm
tmpfs                901M   33M  868M    4% /run
tmpfs                901M     0  901M    0% /sys/fs/cgroup
/dev/mapper/cl-root   76G  3.5G   73G    5% /
/dev/nvme0n1p1       190M  141M   36M   80% /boot
tmpfs                181M     0  181M    0% /run/user/0
/dev/sr0             7.7G  7.7G     0  100% /mnt/cdrom
[root@centos6 ~]#



###

例子五:(统计文件内容的行数)

[root@centos6 ~]# cat xx.py
#coding=utf-8

count_1 = -1

Path_1 = "/root/1.txt"
for count_1, line_1 in enumerate(open(Path_1, 'rU')):
        pass
count_1 = count_1 + 1

print(count_1)
print(type(count_1))


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
9
<class 'int'>
[root@centos6 ~]#



######

例子六:(获取文件内容中第N行的某个数据)

[root@centos6 ~]# cat xx.py
#coding=utf-8

Path_1 = "/root/1.txt"

f_name = open(Path_1,'r') #读取文件内容
Key_1 = f_name.readlines()[6] #读取文件的第7行内容;输出结果为列表,连换行符也会显示出来
f_name.close()

print(Key_1)

print("-" * 10)
Key_2 = Key_1.split()[3] #以空格为分隔符进行分割后,再输出第4项
print(Key_2)
print(f"分区/boot的可用量为{Key_2}")

print("-" * 10)
Key_3 = Key_2[:-1] #去掉最后1个字符
print(Key_3)
print(type(Key_3))

Key_4 = int(Key_3) #转换为整数
print(Key_4)
print(type(Key_4))


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
/dev/nvme0n1p1       190M  141M   36M   80% /boot

----------
36M
分区/boot的可用量为36M
----------
36
<class 'str'>
36
<class 'int'>
[root@centos6 ~]#



######

例子七:(获取文件内容中包含关键字的那行的某个数据)

[root@centos6 ~]# cat xx.py
#coding=utf-8

Path_1 = "/root/1.txt"

f_name = open(Path_1,'r') #读取文件内容
fields_1 = f_name.readlines() #输出结果为列表,连换行符也会显示出来
f_name.close()

K_1 = "cl-root" #关键字

for Key_1 in fields_1:
        if K_1 in Key_1: ##这里相当于模糊查询(in)
                pass
                break #只是结束整个for循环,不是结束程序

print("找到匹配的那行记录:")
print(Key_1)

print("-" * 10)
Key_2 = Key_1.split()[4] #以空格为分隔符进行分割后,再输出第5项
print(Key_2)
print(f"根分区的使用率为{Key_2}")

print("-" * 10)
Key_3 = Key_2[:-1] #去掉最后1个字符
print(Key_3)
print(type(Key_3))

Key_4 = int(Key_3) #转换为整数
print(Key_4)
print(type(Key_4))


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
找到匹配的那行记录:
/dev/mapper/cl-root   76G  3.5G   73G    5% /

----------
5%
根分区的使用率为5%
----------
5
<class 'str'>
5
<class 'int'>
[root@centos6 ~]#



######

例子八:(获取文件内容中以关键字开头的那行的某个数据)

[root@centos6 ~]# cat xx.py
#coding=utf-8

Path_1 = "/root/1.txt"

f_name = open(Path_1,'r') #读取文件内容
fields_1 = f_name.readlines() #输出结果为列表,连换行符也会显示出来
f_name.close()

K_1 = "/dev/mapper/cl-root" #关键字

for Key_1 in fields_1:
        if Key_1.startswith(K_1): ##判断数据是不是以关键字开头
                pass
                break #只是结束整个for循环,不是结束程序

print("找到匹配的那行记录:")
print(Key_1)

print("-" * 10)
Key_2 = Key_1.split()[4] #以空格为分隔符进行分割后,再输出第5项
print(Key_2)
print(f"根分区的使用率为{Key_2}")

print("-" * 10)
Key_3 = Key_2[:-1] #去掉最后1个字符
print(Key_3)
print(type(Key_3))

Key_4 = float(Key_3) #转换为浮点数
print(Key_4)
print(type(Key_4))


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
找到匹配的那行记录:
/dev/mapper/cl-root   76G  3.5G   73G    5% /

----------
5%
根分区的使用率为5%
----------
5
<class 'str'>
5.0
<class 'float'>
[root@centos6 ~]#



######

例子九:(获取文件内容中以关键字结尾的那行的某个数据)

[root@centos6 ~]# cat xx.py
#coding=utf-8

Path_1 = "/root/1.txt"

f_name = open(Path_1,'r') #读取文件内容
fields_1 = f_name.readlines() #输出结果为列表,连换行符也会显示出来
f_name.close()

K_1 = "/boot" #关键字

for Key_1 in fields_1:
        if Key_1.strip().endswith(K_1): ##判断数据是不是以关键字结尾,要先去掉换行符
                pass
                break #只是结束整个for循环,不是结束程序

print("找到匹配的那行记录:")
print(Key_1)

print("-" * 10)
Key_2 = Key_1.split()[4] #以空格为分隔符进行分割后,再输出第5项
print(Key_2)
print(f"分区/boot的使用率为{Key_2}")

print("-" * 10)
Key_3 = Key_2[:-1] #去掉最后1个字符
print(Key_3)
print(type(Key_3))

Key_4 = float(Key_3) #转换为浮点数
print(Key_4)
print(type(Key_4))


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
找到匹配的那行记录:
/dev/nvme0n1p1       190M  141M   36M   80% /boot

----------
80%
分区/boot的使用率为80%
----------
80
<class 'str'>
80.0
<class 'float'>
[root@centos6 ~]#



######

例子十:(使用匿名函数获取文件内容中以关键字结尾的那行的某个数据)

[root@centos6 ~]# cat xx.py
#coding=utf-8

Path_1 = "/root/1.txt"

f_name = open(Path_1,'r') #读取文件内容
fields_1 = f_name.readlines() #输出结果为列表,连换行符也会显示出来
f_name.close()

K_1 = "/boot" #关键字
Key_1 = [item for item in filter(lambda x:x.strip().endswith(K_1),fields_1)]
print(Key_1[0])

print("-" * 10)
Key_2 = Key_1[0][:-7] #去掉最后7个字符(一个换行符算一个字符)
print(Key_2)

print("-" * 10)
Key_3 = Key_2[-3:] #输出最后3个字符
print(Key_3)
print(f"分区/boot的使用率为{Key_3}")


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
/dev/nvme0n1p1       190M  141M   36M   80% /boot

----------
/dev/nvme0n1p1       190M  141M   36M   80%
----------
80%
分区/boot的使用率为80%
[root@centos6 ~]#



######

例子十一:(使用匿名函数获取文件内容中以关键字结尾的那行的某个数据)

[root@centos6 ~]# cat xx.py
#coding=utf-8

Path_1 = "/root/1.txt"

f_name = open(Path_1,'r') #读取文件内容
fields_1 = f_name.readlines() #输出结果为列表,连换行符也会显示出来
f_name.close()

K_1 = "/boot" #关键字
Key_1 = [item for item in filter(lambda x:x.strip().endswith(K_1),fields_1)]
print(Key_1[0])

print("-" * 10)
Key_2 = Key_1[0].split()[4] #以空格为分隔符进行分割后,再输出第5项
print(Key_2)
print(f"分区/boot的使用率为{Key_2}")


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
/dev/nvme0n1p1       190M  141M   36M   80% /boot

----------
80%
分区/boot的使用率为80%
[root@centos6 ~]#





相关文章:
split()函数
字典
if语句
while循环语句

Python3命令集
Python3获取主机名和IP地址
文件操作(创建、读取、写入、追加)

CentOS8使用Python3脚本管理MariaDB
Python3脚本管理Linux下的MySQL
Python3脚本管理MSSQL2014

网络爬虫_爬(blog.zhuohua.store)

返回列表