class XXFile:
def __init__( self, FileName ):
self.File = FileName
def Process ( self ):
for self.Line in open ( self.File ):
self.SetFlds()
def SetFlds ( self ):
Write2Log ( "Inside the SetFlds()->Line::[" + self.Line + "]" )
Write2Log ( "Test->Line22,34::[" + self.Line [ 22 : 34 ].strip() + "]" )
MyFile = XXFile( "a.txt" )
MyFile.Process()
输出
2014-02-26T20:41:47| Inside the SetFlds()->Line::[XXXX 9999999 XXXXXXXXXXXXXXXXXXXXXXX ABCDE]
2014-02-26T20:41:47| Test->Line22,34::[]
为什么我不能得到长度为34的22个字符?我已经把所有的角色都写满了林先生在setflds()中,但是自动线不起作用。。你知道吗
为什么我没有从22到34的长度?
self.Line[22:34]
从索引位置22开始获取(最多)34-22=12个字符。要获得34个字符,请使用参见此处获取explanation of Python slicing。你知道吗
另外,最好使用
str.split
:您的行上有空格;字符22到34是空格或制表符,
.strip()
调用删除这些字符,留下一个空字符串:相关问题 更多 >
编程相关推荐