splitlinespython
⑴ 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 expandtabs().splitlines(),最近在看python一個計數文件行數的代碼,可是看不懂這個函數的作用
lines=docstring.expandtabs().splitlines()
首先,docstring是字元串(string)。
然後,string.expandtabs()是將字元串裡面的tab製表符換成空格,如果沒有指定tabsize參數,默認一個tab轉化成8個空格。
(這是help裡面的說明:Return a of S where all tab characters are expanded using spaces.If tabsize is not given, a tab size of 8 characters is assumed.)
之後,string.splitlines()是將一串字元串按行分割,並返回分割後的列表(list)。
⑶ Python 計算一個文件中有多少行即讀取文件行數
with open(file) as f:
text=f.read()
length=len(text.splitlines())
⑷ python中splitlines不能給列表用嗎
不能,可以先將列表轉換成字元串再使用
⑸ python獲取執行命令的返回結果
p=subprocess.Popen('psaux',shell=True,stdout=subprocess.PIPE)
out,err=p.communicate()
forlineinout.splitlines():
printline
⑹ 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多行輸入問題
兄弟,你問這個問題太一級。
首先,你必須先學會Python語言,學會了在任何平台上可以進行編程。你必須弄清楚這個概念。
其次,要安裝Python的開發環境。如果你使用Ubuntu是很簡單的。
命令和apt-get安裝蟒蛇
編輯器,我建議你使用Vim和Emacs,神器。 。
三,讀這本書,他試著寫多了,想了想後。
「Lernning Python的」,「編程的Python」我建議你看看這些書。
⑻ linux或Python提取一定范圍的行
usage Python
#!/usr/bin/envpython
#coding:utf-8
"""
"""
importre
context="""
12143214544325
2321443214543
34325432543543
6324344324
5433244324
"""#(行號不連貫,並且有缺失)
rangedefine="""
12
35
"""
defiter_parser_context(strstream):
patt=re.compile(r"^(?P<sno>d+)s+(?P<content>.*)")
forlninstrstream:
matched=patt.match(ln)
ifmatched:
yieldmatched.groupdict()
defgetrangedefine(context):
datarange=[]
forlnincontext.splitlines():
datarange.append(re.findall("d+",ln))
returnfilter(None,datarange)
define=getrangedefine(rangedefine)
fordatainiter_parser_context(context.splitlines()):
ifany([minsno<=data["sno"]<=maxsnofor(minsno,maxsno)indefine]):
print"%(sno)s%(content)s"%data
⑼ python怎麼提取關鍵詞
你好, 那個r'.*?('+ lste +').*?『 會吧你這個關鍵字前面和後面的文字都匹配了,所以當你的那個關鍵字多次出現的時候,就會報那個重復出現的錯了。
你直接
hh = re.findall(lste, gg)就可以了呀?
或者是還有什麼需要匹配的東西,我看你後面好像要將結果連接起來,但是你匹配出來的都是關鍵字,直接連接的話,其實就是多個關鍵字的拼接了。
⑽ python從txt文件中讀取數字,並且判斷其大小。
#!/usr/bin/python
#encoding:utf-8
importre
context="""
0.00000000E+001.99000000E-021.05000000E+000.00000000E+003.88519671E+02
4.20000000E-021.99000000E-021.00800000E+000.00000000E+004.23216155E+02
8.40000000E-021.99000000E-029.66000000E-010.00000000E+004.67920285E+02
1.26000000E-011.99000000E-029.24000000E-010.00000000E+004.89975818E+02
1.68000000E-011.99000000E-028.82000000E-010.00000000E+005.01845166E+02
2.10000000E-011.99000000E-028.40000000E-010.00000000E+005.10017363E+02
2.52000000E-011.99000000E-027.98000000E-010.00000000E+005.15927207E+02
2.94000000E-011.99000000E-027.56000000E-010.00000000E+005.18914324E+02
3.36000000E-011.99000000E-027.14000000E-010.00000000E+005.18683254E+02
"""
patt=re.compile(r"d.d+E[+-]d{2}")
array=[]
forlninfilter(None,context.splitlines()):
array.append(map(float,patt.findall(ln)))
debug=True
defdebugView(obj):
importpprint
ifdebug:
pprint.pprint(obj)
debugView(array)
你的數據來自文本文件,可以用 for ln in filter(None, open(datafile, 'rt'))
替代 for ln in filter(None, context.splitlines())