[英]Python Print text after certain number of blank lines
我有某些格式如下的文本:
text:aaaaaaa
text:bbbbbb
text:cccccc
text:ddddd
text:eeeeee
text:ffffff
我想在以下位置打印文本:
text:fffff
如何編寫一些python代碼來計算文本之間的兩個空格並在text:fffff上打印文本
>>> with open("foo.txt") as fin:
... any(x.isspace() for x in fin) # skip to 1st blank line
... any(x.isspace() for x in fin) # skip to 2nd blank line
... next(fin)
...
True
True
' text:ffffff\n'
跳過n
空行
>>> n = 2
>>> with open("t.txt") as fin:
... for i in range(n):
... any(x.isspace() for x in fin)
... print next(fin)
...
True
True
text:ffffff
為此,請執行以下操作:
mydictonary = dict((k.strip(), v.strip()) for k,v in
(item.split(':') for item in s.split(' ')))
在這里貼上這個東西:
(k.strip(), v.strip())
"<key> ", " <value>"
對: item.split(':')
s.split(' ')
將字符串拆分為"<key> - <value>"
對
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.