Object Oriented Python 简明教程

Object Oriented Python - Files and Strings

Strings

字符串是所有编程语言中最常用的数据类型。这是为什么?因为我们更能理解文本,而不是数字,所以我们在书写和交谈时使用文本和单词,同样,在编程中,我们也使用字符串。在字符串中,我们解析文本、分析文本语义并进行数据挖掘 - 所有这些数据都是人类可理解的文本。Python 中的字符串是不可变的。

String Manipulation

在 Python 中,字符串可以用多种方式标记,对于多行字符串可以使用单引号(')、双引号(")甚至三重引号(''')。

>>> # String Examples
>>> a = "hello"
>>> b = ''' A Multi line string,
Simple!'''
>>> e = ('Multiple' 'strings' 'togethers')

字符串操作非常有用,在每种语言中都得到了非常广泛的使用。通常,程序员需要分解字符串并仔细检查它们。

字符串可以进行迭代(逐个字符)、切片或连接。语法与列表相同。

str 类中包含大量方法,可以使字符串操作变得更容易。dir 和 help 命令在 Python 解释器中提供了如何使用这些方法的指导。

以下是我们使用的一些常用字符串方法。

Sr.No.

Method & Description

1

isalpha() 检查所有字符是否是字母

2

isdigit() Checks Digit Characters

3

isdecimal() Checks decimal Characters

4

isnumeric() checks Numeric Characters

5

find() 返回子字符串的最高索引

6

istitle() Checks for Titlecased strings

7

join() Returns a concatenated string

8

lower() returns lower cased string

9

upper() returns upper cased string

10

partion() Returns a tuple

11

bytearray() 返回给定字节大小的数组

12

enumerate() Returns an enumerate object

13

isprintable() Checks printable character

让我们尝试运行几个字符串方法:

>>> str1 = 'Hello World!'
>>> str1.startswith('h')
False
>>> str1.startswith('H')
True
>>> str1.endswith('d')
False
>>> str1.endswith('d!')
True
>>> str1.find('o')
4
>>> #Above returns the index of the first occurence of the character/substring.
>>> str1.find('lo')
3
>>> str1.upper()
'HELLO WORLD!'
>>> str1.lower()
'hello world!'
>>> str1.index('b')
Traceback (most recent call last):
   File "<pyshell#19>", line 1, in <module>
      str1.index('b')
ValueError: substring not found
>>> s = ('hello How Are You')
>>> s.split(' ')
['hello', 'How', 'Are', 'You']
>>> s1 = s.split(' ')
>>> '*'.join(s1)
'hello*How*Are*You'
>>> s.partition(' ')
('hello', ' ', 'How Are You')
>>>

String Formatting

在 Python 3.x 中,字符串的格式发生了变化,现在更合乎逻辑且更灵活。可以在格式字符串中使用 format() 方法或 % 符号(旧样式)进行格式化。

字符串可以包含文字或用大括号 {} 括起来的替换字段,每个替换字段都可以包含位置参数的数字索引或关键字参数的名称。

syntax

str.format(*args, **kwargs)

Basic Formatting

>>> '{} {}'.format('Example', 'One')
'Example One'
>>> '{} {}'.format('pie', '3.1415926')
'pie 3.1415926'

以下示例允许重新调整显示顺序而不更改参数。

>>> '{1} {0}'.format('pie', '3.1415926')
'3.1415926 pie'

字符串填充和对齐

可以将值填充到特定长度。

>>> #Padding Character, can be space or special character
>>> '{:12}'.format('PYTHON')
'PYTHON '
>>> '{:>12}'.format('PYTHON')
' PYTHON'
>>> '{:<{}s}'.format('PYTHON',12)
'PYTHON '
>>> '{:*<12}'.format('PYTHON')
'PYTHON******'
>>> '{:*^12}'.format('PYTHON')
'***PYTHON***'
>>> '{:.15}'.format('PYTHON OBJECT ORIENTED PROGRAMMING')
'PYTHON OBJECT O'
>>> #Above, truncated 15 characters from the left side of a specified string
>>> '{:.{}}'.format('PYTHON OBJECT ORIENTED',15)
'PYTHON OBJECT O'
>>> #Named Placeholders
>>> data = {'Name':'Raghu', 'Place':'Bangalore'}
>>> '{Name} {Place}'.format(**data)
'Raghu Bangalore'
>>> #Datetime
>>> from datetime import datetime
>>> '{:%Y/%m/%d.%H:%M}'.format(datetime(2018,3,26,9,57))
'2018/03/26.09:57'

Strings are Unicode

字符串作为不可变 Unicode 字符的集合。Unicode 字符串提供了创建可在任何地方运行的软件或程序的机会,因为 Unicode 字符串可以表示任何可能的字符,而不仅仅是 ASCII 字符。

即使字节对象引用文本数据,许多 IO 操作也只知道如何处理字节。因此,了解如何在字节和 Unicode 之间进行互换非常重要。

将文本转换为字节

将字符串转换为字节对象称为编码。有许多形式的编码,最常见的是:PNG;JPEG、MP3、WAV、ASCII、UTF-8 等。此外,(编码)是一种以字节表示音频、图像、文本等的格式。

这种转换可以通过 encode() 实现。它将编码技术作为参数。默认情况下,我们使用“UTF-8”技术。

>>> # Python Code to demonstrate string encoding
>>>
>>> # Initialising a String
>>> x = 'TutorialsPoint'
>>>
>>> #Initialising a byte object
>>> y = b'TutorialsPoint'
>>>
>>> # Using encode() to encode the String >>> # encoded version of x is stored in z using ASCII mapping
>>> z = x.encode('ASCII')
>>>
>>> # Check if x is converted to bytes or not
>>>
>>> if(z==y):
   print('Encoding Successful!')
else:
   print('Encoding Unsuccessful!')
Encoding Successful!

Converting bytes to text

将字节转换为文本称为解码。这是通过 decode() 实现的。如果我们知道哪个编码用于对其进行编码,则可以将字节字符串转换为字符字符串。

因此,编码和解码是逆向过程。

>>>
>>> # Python code to demonstrate Byte Decoding
>>>
>>> #Initialise a String
>>> x = 'TutorialsPoint'
>>>
>>> #Initialising a byte object
>>> y = b'TutorialsPoint'
>>>
>>> #using decode() to decode the Byte object
>>> # decoded version of y is stored in z using ASCII mapping
>>> z = y.decode('ASCII')
>>> #Check if y is converted to String or not
>>> if (z == x):
   print('Decoding Successful!')
else:
   print('Decoding Unsuccessful!') Decoding Successful!
>>>

File I/O

操作系统将文件表示为字节序列,而不是文本。

文件是磁盘上的一个已命名位置,用于存储相关信息。它用于永久存储磁盘中的数据。

在 Python 中,文件操作按以下顺序进行。

  1. Open a file

  2. 从文件(操作)中读取或写入。打开文件

  3. Close the file.

Python 用适当的解码(或编码)调用包装传入(或传出)字节流,以便我们可以直接处理 str 对象。

Opening a file

Python 有一个内置函数 open() 来打开文件。这将生成一个文件对象,也称为句柄,因为它用于相应地读取或修改文件。

>>> f = open(r'c:\users\rajesh\Desktop\index.webm','rb')
>>> f
<_io.BufferedReader name='c:\\users\\rajesh\\Desktop\\index.webm'>
>>> f.mode
'rb'
>>> f.name
'c:\\users\\rajesh\\Desktop\\index.webm'

要从文件中读取文本,我们只需要将文件名传递给函数。系统将打开该文件以进行读取,并使用平台默认编码将字节转换为文本。