[英]Python reading large file and spliting after :
Based on this example of a line from a file 基于此文件中的行的示例
1:alpha:beta
I'm trying to get python to read the file in and then line by line print whats after the 2nd ':'
我试图让python读取文件,然后逐行打印第二个':'
后面的':'
import fileinput
#input file
x = fileinput.input('ids.txt')
strip_char = ":"
for line in x:
strip_char.join(line.split(strip_char)[2:])
This produces no results, however from a console session on a single line it works fine 这不会产生任何结果,但是从一行的控制台会话中它可以正常工作
Python 2.7.3rc2 (default, Apr 22 2012, 22:35:38)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
data = '1:alpha:beta'
strip_char = ":"
strip_char.join(data.split(strip_char)[2:])
'beta'
What am i doing wrong please? 我做错了什么? Thanks 谢谢
Values returned by functions aren't automatically sent to stdout in non-interactive mode, you have to explicitly print them. 函数返回的值不会在非交互模式下自动发送到stdout,您必须显式打印它们。
So, for Python 2, use print line.split(strip_char, 2)[2]
. 因此,对于Python 2,使用print line.split(strip_char, 2)[2]
。 If you ever use Python 3, it'll be print(line.split(strip_char, 2)[2])
. 如果你曾经使用过Python 3,它将是print(line.split(strip_char, 2)[2])
。
(Props to Jon Clements, I forgot you could limit how many times a string will be split.) (对于Jon Clements的道具,我忘了你可以限制一个字符串分裂的次数。)
For the data format given this will work: 对于给定的数据格式,这将起作用:
with open('data.txt') as inf:
for line in inf:
line = line.strip()
line = line.split(':')
print ':'.join(line[2:])
For '1:alpha:beta'
the output would be 'beta'
对于'1:alpha:beta'
,输出将为'beta'
For '1:alpha:beta:gamma'
the output would be 'beta:gamma'
(Thanks for @JAB pointing this out) 对于'1:alpha:beta:gamma'
,输出将为'beta:gamma'
(感谢@JAB指出这一点)
If it's everything after the 2nd ':' as a string (which can include ':') then use the maxsplit option, eg: 如果它是第二个':'之后的所有字符串(可以包含':'),那么使用maxsplit选项,例如:
line.split(':', 2)[2]
eg: 例如:
>>> d = '1:alpha:beta:charlie:delta'
>>> d.split(':', 2)
['1', 'alpha', 'beta:charlie:delta']
This saves joining afterwards 这节省了之后的加入
You get just 'beta' because join gives you a string: 你得到'beta'因为join给你一个字符串:
data = '1:alpha:beta'
strip_char = ":"
strip_char.join(data.split(strip_char)[2:])
'beta'
Try this: 尝试这个:
lines=[]
with open('filePath', 'r') as f:
for line in f.readlines():
lines.append(line.strip())
for line in lines: print line.split(':')[1:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.