⑴ python3.x 截取指定字元串應該怎麼寫
defread2memory(file_path):
withopen(file_path,'r',encoding='utf8')asf:
returnf.read().split(' ')
a=read2memory("test.txt")
print(a[0].split('Helvetica')[1].split('<br/>')[0])
⑵ python 怎麼將字元串分割
固定長度分割,直接通過[:3] 這種來取。
固定分隔符一般用split
看你需求,其他的方式也有。最好有個例子。
⑶ python如何把一個字元串批量切割並轉化成圖片
當然可以。
先根據字元的長度,將字元分成N個組,每組一個字元。
然後根據電腦顯示器的大小,創建一個圖片框,高度和長度分別設置為電腦顯示器的25%。
再將字元顯示到圖片框中,保存圖片框的內容為圖片文件即可。
⑷ python字元串(特殊字元,取值,常用方法)
1.字元串
特殊字元串
\n:換行
\r:刪除\r前面的字元
\t:製表符
例如:
s_1 = "人生苦短,\n我選Python!"
s_2 = "人生苦短,\r我選Python!"
s_3 = "人生苦短,\t我選Python!"
print(s_1) # 人生苦短,
print(s_2) # 我選Python
print(s_3) # 人生苦短, 我選Python!
遇到特殊字元,想去掉效果,把特殊字元轉成普通字元
可以使用# r R
s_1 =r "人生苦短,\n我選Python!"
s_2 =R "人生苦短,\r我選Python!"
s_3 = "人生苦短,\t我選Python!"
2.字元串取值
特點:取頭不取尾,正序從0開始,倒序從-1開始
[start:end:step] #step:表示間隔
s='hello python lemon'
print(s[6:12:1]) #正序 python 6,7,8,9,10,11
print(s[-12:-6:1]) # 倒序 python -12,-11,-10,-9,-8,-7
print(s[:])#hello python lemon 從頭取到尾 [:]
print(s[6:]) #python lemon 從6取到尾 [start:]
print(s[:17])# [:end] 從開始取到16
獲取s所有的偶數位的字母
print(s[0:17:2])
獲取s所有的奇數位的字母
print(s[1:18:2])
倒序輸出所有的字母
print(s[17::-1]) # 不可以寫出是s[17:-1:-1] or s[17:0:-1]
3.常用方法
find() : 返回-1表示未找到子字元串,找到會返回對應字元的索引,子字元包含單個字元或多個字元
isdigit():判斷是否全部是數字,是返回True,否返回False
replace(要替換的內容:替換的內容:替換的次數):指定替換內容以及被替換的字元串,並可以指定替換次數,默認是全部替換
split(指定字元,指定切割的次數):根據指定字元對字元串進行切割,默認全部切割
strip():去掉頭和尾指定的字元
upper():字元串的字母轉成大寫
lower():字元串的字母轉成小寫
swapcase():字元串的字母大小互換
例如:
s='learn python in lemon'
print(s.find('n')) #返回找到字元串的索引
print(s.find(python))#返回找到的子字元串的第一個索引值--6
print(s.find('k')) # 返回-1
print(s.find('o',11))#從索引值為11的值開始找---19
print(s.isdigit())# 返回False
s1 = "******learn python*****"
print(s.strip("*"))# learn python
⑸ Python3中操作字元串str必須記住的幾個方法
split([sep]) 將字元串分割為列表,默認用空白符分割,給出字元串參數,用參數字元串分割
'a b c'.split() 返回 ['a','b','c']
join 將可迭代對象中的字元串連接在一起
'\n'.join(['a','b','c'] )返回字元串 "a\nb\nc"
str.find(substr,[start,[end]]) 從str的下標 start至end之間查找substr,返回substr出現位置的下標,未找到返回-1
str.index 與find相仿,但未找到拋出異常
其餘還要通用的下標 ,切片操作等
⑹ python中分割字元串
imkow正解,直接轉list最好,否則自己寫list comprehension其實隱含的還是把字元串當list用,多此一舉
⑺ Python 字元串切割(str.split 和 re.split()的區別)
前言: str.split() 和 re.split() 都可以用作字元串的切割,區別是:
⑻ python3 有規律的字元串怎麼拆分
root@localhost:~/xly/02# cat t.py
a=[{'address': '172.16.20.1', 'port': 80}, {'address': '172.16.20.2', 'port': 80}, {'address': '172.16.20.3', 'port': 80}]
for i in a:
print "%s:%s"%(i['address'],i['port'])
root@localhost:~/xly/02# python t.py
172.16.20.1:80
172.16.20.2:80
172.16.20.3:80
⑼ python3中通過遇到的第一個指定字元串來切割字元串
用split這個函數就行,很簡單的
>>>s="qaz>123>wsx"
>>>s.split('>')
['qaz','123','wsx']
>>>
⑽ 使用Python按位元組分割字元串
按行讀取之後按原文件編碼類型解碼,插入完後按UTF-8解碼寫入文件
以源文件為gbk為例,假設每5字元插入|
python2
withopen('target','w')asf:
forlineopen('source').readlines():
line=line.decode('gbk')
line='|'.join([line[i:min(i+5,len(line))]foriinrange(0,len(line),5)])
f.write(line.encode('utf-8'))
python3
withopen('target','w',encoding='utf-8')asf:
forlineopen('source',encoding='gbk').readlines():
line=line
line='|'.join([line[i:min(i+5,len(line))]foriinrange(0,len(line),5)])
f.write(line)