1

I have this program:

import sys
import itertools
from itertools import islice

fileLocation = input("Input the file location of ScoreBoard: ")

input1 = open(fileLocation, "rt")

amountOfLines = 0
for line in open('input1.txt').readlines(  ):
    amountOfLines += 1

timestamps = [line.split(' ', 1)[0][0:] for line in islice(input1, 2, amountOfLines)]
teamids = [line.split(' ', 1)[0][0:] for line in islice(input1, 2, amountOfLines)]

print(teamids)

and this text file:

1
5 6
1 5 1 5 0
1 4 1 4 1
2 1 2 1 1
2 2 3 1 1
3 5 2 1 1
4 4 5 4 1

For teamids, I want it to start reading after the first space and to the next space, starting from the second line which, I have already achieved but don't get how to start reading after the first space to the next. For timestamps i have managed this but only starting from the first character to the first space and don't know how to do this for teamids. Much help would be appreciated

vapouryh
  • 41
  • 6
  • 1
    Could you describe the input format in more detail? It's probably much easier to parse each line as you read it, rather than trying to read the entire file into memory and doing ... whatever it is you are doing. – chepner Feb 23 '21 at 19:06
  • its a scoreboard, I have different input files that I want the code to work for as I specify, some are very long. in the text file, teamids is on the second column starting after the first space going from 5, 4, 1... i just want to get those numbers and save to a list, like how I did with timestamps – vapouryh Feb 23 '21 at 19:11
  • Rather than iterating over the entire file contents repeatedly, consider extracting all the values from a single line, then appending each to the appropriate list. – chepner Feb 23 '21 at 19:14
  • amountOfLines = len(open('input1.txt').readlines( )) whould be shorter solution (though it has nothing to do with original question) you can use file object as an iterator of lines, but only once. your teamids will be empty as input1 is already exhausted. – user3431635 Feb 23 '21 at 19:16
  • But in my other input files some of these digits are more than 9 eg 10, 11, 12. I am using this method because it takes all the digits before the first space and saves them to a list – vapouryh Feb 23 '21 at 19:27
  • Fixed By: ```py import sys import itertools from itertools import islice fileLocation = input("Input the file location of ScoreBoard: ") timestamps = [] teamids = [] amountOfLines = len(open('input1.txt').readlines()) with open('input1.txt') as input1: for line in islice(input1, 2, amountOfLines): parsed = line.strip().split() timestamps.append(parsed[0]) teamids.append(parsed[1]) print(timestamps) ``` – vapouryh Feb 23 '21 at 19:39

1 Answers1

1

Here's one suggestion showcasing a nice use case of zip to transpose your array:

lines = open(fileLocation, 'r').readlines()[2:]
array = [[int(x) for x in line.split()] for line in lines]
transpose = list(zip(*filter(None, array)))

# now we can do this:
timestamps = transpose[0]  # (1, 1, 2, 2, 3, 4)
teamids = transpose[1]     # (5, 4, 1, 2, 5, 4)

This exploits the fact that zip(*some_list) returns the transpose of some_list.

Beware of the fact that the number of columns you get will be equal to the length of the shortest row. Which is one reason why I included the call to filter to remove empty rows caused by empty lines.

BizarreCake
  • 662
  • 5
  • 14