Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python例外索引问题?_Python - Fatal编程技术网

Python例外索引问题?

Python例外索引问题?,python,Python,我有一个以制表符分隔的文件,其中有8列,例如具有以下上下文 Timestamp Process TID Area Category EventID Level Message Correlation 06/21/2014 09:19:02.94 wsstracing.exe (0x068C) 0x0698 SharePoint Foundation

我有一个以制表符分隔的文件,其中有8列,例如具有以下上下文

Timestamp  Process   TID        Area            Category     EventID    Level           Message         Correlation 

06/21/2014 09:19:02.94  wsstracing.exe (0x068C)                         0x0698  SharePoint Foundation           Tracing Controller Service      5152    Information     Tracing Service started.     

06/21/2014 09:19:09.94  hostcontrollerservice.exe (0x063C)              0x0670  SharePoint Server               Unified Logging Service         b8fx    High            ULS Init Completed (hostcontrollerservice.exe, Microsoft.Office.Server.Native.dll)         

我目前拥有的代码

try:
    with open('text.log') as f:
        for l in f:
            print(l.strip().split("\t")[5], end=" "),
            print(l.strip().split("\t")[7], end=" "),
            print(l.strip().split("\t")[8], end="\n")
except IndexError:
    pass
给我

EventID Message  Correlation

5152 Tracing Service started. Press any key to continue . . .
正如您所看到的,它在第一个条目之后停止,因为第8列中不再有任何内容

当我需要打印的时候,即使相关列下没有任何内容

EventID          Message              Correlation

1124             blahblahblah         blahblah
但是当我有以下代码时

try:
    with open('text.log') as f:
        for l in f:
            print(l.strip().split("\t")[5], end=" "),
            print(l.strip().split("\t")[7])

但是要以正确的格式打印,有人能提供帮助吗?

为什么要将整个内容包装在
try
块中

with open('text.log') as f:
    for line in f:
        txt = l.strip().split("\t") # split once
    for col in (5,7,8):
        try:
            _end = "\n" if col == 8 else " "
            print(txt[col], end=_end)
        except IndexError:
            print("\t", end=_end) # print a blank column

当您执行
.strip()
操作时,您将丢失尾随选项卡,因此当您执行
拆分()
操作时,该行会更短。除此之外,您还多次执行相同的操作

请尝试以下方法:

    with open('text.log') as f:
        for l in f:
            fields = l.split('\t')
            print(fields[5], end=" "),
            print(fields[7], end=" "),
            print(fields[8], end="\n")

您的
try
被环绕在循环中,因此一旦出现一个错误,
try
块将停止执行并跳转到
块之外的
块,这意味着不会再发生迭代

相反,您只需将
try
/
放入for循环中即可

with open('text.log') as f:
    for l in f:
        try:
            print(l.strip().split("\t")[5], end=" "),
            print(l.strip().split("\t")[7], end=" "),
            print(l.strip().split("\t")[8], end="\n")
        except IndexError:
            pass
然而,因为您知道不可能有第8个元素,所以它并不真正例外,如果您没有第6或第7个元素,它将隐藏错误

相反,尝试通过以下方式更好地控制您的逻辑:

with open('text.log') as f:
    for l in f:
        x = l.strip().split("\t")[5:] # Grab the elements you want...
        x.pop(1) #... but remove the element you don't
        print(" ".join(x)) # Now print them

而不是在你的问题中使用
pastebin
链接发布5-10行数据。@Ethan Furman好的,我不知道如何正确设置这里的格式。谢谢@jwodder提供正确的格式。这也有同样的问题,因为如果元素
8
不存在,它会抛出错误,阻止进一步执行。如果不使用
strip
并丢失尾随选项卡,所有八个元素都将在那里。这似乎会弄乱格式?它现在以打印为基础基金会基金会而不是SharePointFoundation@user3788715:抱歉--您仍然希望拆分中的
'\t'
。我明白了!非常感谢你的帮助!pop将根据我选择的元素工作,对吗?所以如果我想要5-8,我放1,它会移除第6个元素吗?