分裂在“:”上 [英] spliting on ":"
问题描述
嗨
i有一个文件
xxx.xxx.xxx.xxx:yyy
xxx .xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx
xxx。 xxx.xxx.xxx
xxx.xxx.xxx.xxx:yyy
i想分开:"得到所有的yyy打印出整行
所以我做了
print line.split(":")[ - 1]
但不打印第4行和第5行,因为没有:分开。它应该
打印出一个空白字样。至少。
如何打印第4和第5行?
例如输出是
yyy
yyy
yyy
yyy
谢谢
hi
i have a file with
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx:yyy
i wanna split on ":" and get all the "yyy" and print the whole line out
so i did
print line.split(":")[-1]
but line 4 and 5 are not printed as there is no ":" to split. It should
print a "blank" at least.
how to print out lines 4 and 5 ?
eg output is
yyy
yyy
yyy
yyy
thanks
推荐答案
s9 ************ @ yahoo .com 写道:
s9************@yahoo.com wrote:
print line.split(":")[ - 1]
但第4和第5行不是因为没有:而打印分开。它应该打印一个空白字样。至少。
如何打印第4和第5行?
例如输出是什么?yyy
yyy
yyy
yyy
print line.split(":")[-1]
but line 4 and 5 are not printed as there is no ":" to split. It should
print a "blank" at least.
how to print out lines 4 and 5 ?
eg output is
yyy
yyy
yyy
yyy
if":"在线:
print line.split(":")[ - 1]
else:
print
有什么问题?
if ":" in line:
print line.split(":")[-1]
else:
print
what''s the problem?
s9 ************ @ yahoo.com 写道:
嗨
我有一个文件
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx:yyy
xxx.xxx xxx.xxx.xxx
xxx.xxx.xxx.xxx:yyy
得到所有的yyy并打印整行
所以我做了
打印line.split(":)[ - 1]
但第4和第5行是不打印,因为没有:分开。它应该打印一个空白字样。至少。
如何打印第4和第5行?
例如输出是什么?yyy
yyy
yyy
yyy
hi
i have a file with
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx:yyy
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx:yyy
i wanna split on ":" and get all the "yyy" and print the whole line out
so i did
print line.split(":")[-1]
but line 4 and 5 are not printed as there is no ":" to split. It should
print a "blank" at least.
how to print out lines 4 and 5 ?
eg output is
yyy
yyy
yyy
yyy
这不是我得到的:
在[2]中:data =''''' 'xxx.xxx.xxx.xxx:yyy
...:xxx.xxx.xxx.xxx:yyy
...:xxx.xxx.xxx.xxx :yyy
...:xxx.xxx.xxx.xxx
...:xxx.xxx.xxx.xxx
.. 。:xxx.xxx.xxx.xxx:yyy''''''。splitlines()
在[3]中:对于数据中的行:
...:print line.split('':'')[ - 1]
......:
......:
yyy
yyy
yyy
xxx.xxx.xxx.xxx
xxx.xxx.xxx。 xxx
yyy
Kent
That''s not what I get:
In [2]: data=''''''xxx.xxx.xxx.xxx:yyy
...: xxx.xxx.xxx.xxx:yyy
...: xxx.xxx.xxx.xxx:yyy
...: xxx.xxx.xxx.xxx
...: xxx.xxx.xxx.xxx
...: xxx.xxx.xxx.xxx:yyy''''''.splitlines()
In [3]: for line in data:
...: print line.split('':'')[-1]
...:
...:
yyy
yyy
yyy
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx
yyy
Kent
Cyril Bazin写道:
Cyril Bazin wrote:
您的文件看起来像是IP地址列表。
您可以使用urllib和urllib2模块来解析IP地址。
导入urllib2
for line in open(" fileName。 TXT"):<无线电通信/> addr,port = urllib2.splitport(line)
print(port!= None)和''''或port
Your file looks like a list of IP adresses.
You can use the urllib and urllib2 modules to parse IP adresses.
import urllib2
for line in open("fileName.txt"):
addr, port = urllib2.splitport(line)
print (port != None) and '''' or port
这就是你的意思想要在端口为None时发生?
Is this what you want to happen when port is None?
port = None
print(port!= None)和''''或端口
port = None
print (port != None) and '''' or port
无
我想你会被经典和/或陷阱用Python,
试图避免使用简单的if语句。
顺便说一下,与None的相等比较通常是一个坏主意,因为
好吧,所以即使以上工作也应该(端口不是None)而不是(b!没有)
。
-Peter
None
I think you''re getting caught by the classic and/or trap in Python,
trying to avoid using a simple if statement.
By the way, equality comparison with None is generally a bad idea as
well, so even if the above worked it should be (port is not None) rather
than (port != None).
-Peter
这篇关于分裂在“:”上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!