python字元串分割

格式復太亂了制,給你個參考吧
import re
s = 'type <unsigned int>\nport_num:4\nport:<in_port><sc_out<sc_uint<4>>>'
a = s.split('\n')
ok = []
for i in a:
if re.match('>',i[len(i)-1]):
print(i[:len(i)-1].replace('><',',').replace(':<',',').replace(' <',','))
else:
print(i.replace(':',','))

㈡ python如何用分割符把字元串變列表

str[0].split(".") #str[0]取出第一個字元串,然後利用split()函數,用分隔符"."將字元串變回為列表。答
str[0].split(",")#用分隔符","將字元串變為列表。
str[0].split(" ") #用分隔符" "(空格),將字元串變為列表。

㈢ python怎樣將字元串按行分割成列表

字元串.split()
split()就是分隔符的意思,括弧里就是你設置的分隔符。

㈣ python中分割字元串

list(a)得['1', '2', '3', '4']

㈤ python 字元串分割截取

>>>
10.1.1.100,10.2.100.125,189.10.2.40,189.0.100.50
10.1.1.1,10.2.100.1,189.10.2.1,189.0.100.1str='10.1.1.100,10.2.100.125,189.10.2.40,189.0.100.50'
print(str)
str=str.split(',')
str1=[]
forelementinstr:
list=element.split('.')
list[len(list)-1]='1'
list='.'.join(list)
str1.append(list)
str1=','.join(str1)
print(str1)

㈥ 使用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)

㈦ python字元串拆分

|print ' '.join(dict.split(','))

key和value之間想用什麼分隔就用什麼分隔。比如用『 | 』那麼就:

print ' | '.join(dict.split(','))

...

如果有其他特殊的要求,請LZ闡述清楚。

㈧ python字元串分割問題

在平時工作的時候,發現對於字元串分割的方法用的比較多,下面對分割字元串方法進行總結一下:
第一種:split()函數
split()函數應該說是分割字元串使用最多的函數
用法:
str.split('分割符')
通過該分割操作後,會返回一個列表。
註:當然如果你的字元串含有一個或者多個空格就直接 str.split() 就可以了
例如:
>>> a = "hello,python,Good Night"
>>> a.split(',')
['hello', 'python', 'Good Night']

第二種:splitlines()函數
splitline()函數是按「行」進行字元串分割
用法:
object.splitlines()
通過該分割操作後,會返回一個列表。
例如:
>>> a = '''I have a pen
I have a apple
apple pen
'''
>>> a.splitlines()
['I have a pen','I have a apple','apple pen']

㈨ python如何拆分含有多種分隔符的字元串

通過re.split()方法,一次性拆分所有字元串
import re
def go_split(s, symbol):
# 拼接正則表達式
symbol = "[" + symbol + "]+"
# 一次性分割字元串
result = re.split(symbol, s)
# 去除空字元
return [x for x in result if x]
if __name__ == "__main__":
# 定義初始字元串
s = '12;;7.osjd;.jshdjdknx+'
# 定義分隔符
symbol = ';./+'
result = go_split(s, symbol)
print(result)

㈩ python按大小分割字元串

沒用理解按大小分割的意思,大概是按指定長度分割吧?
比較直接的方法:
# 比如7個字元分割
c =7
s ='asdfaddsfgsdfgdsfgsdfg'
print [s[i:i+c] for i in xrange(0,len(s),c)]