iheartcpp iheartcpp - 1 year ago 131
Python Question

Copying partial list in python

I'm trying to collect the output from a system command that lists tickets (strings) separated by new lines. I specify what 'lastTicket' is and I want to copy the full list until I reach lastTicket. I'm getting the following error, "TypeError: list indices must be integers, not str" on line 5 below. How can I copy only the portion of the list that I need? Thanks.

output = subprocess.check_output(cmd, shell=True)

allTickets = output.rstrip().split('\n')

idx = len(allTickets)
for ii in allTickets:
if lastTicket in allTickets[ii]:
idx = ii

print idx

tickets = allTickets[idx:len(allTickets)]

Answer Source

use either

for ii in range(len(allTickets)):
    if lastTicket in allTickets[ii]:
        idx = ii

BTW are you sure you want if lastTicket in allTickets[ii]: and not if lastTicket == allTickets[ii]:?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download