-1

For some reason the second loop in this piece of python code seems to want to skip over for no reason I don't know why but it reads the first line of the for loop and then skips it without executing any of the code inside.

For some reason removing the first for loop allows the second one to run and I don't why or how.



input("nothing")

with open ("Rainbow Six Siege Survey.csv") as csv_file:
    csv_reader = csv.DictReader(csv_file)

    line_count = 0

    WinLoss = []
    KillDeath = []
    Names = []
    Time = []

    for i in range(0, len(list(csv_reader))):
        WinLoss.append(0)
        KillDeath.append(0)
        Names.append("")
        Time.append("")


    for row in csv_reader:
        #if line_count == 0:
            #print("The columns are: {",".join(row)}")
        #else:
            #Names[line_count-1] = row["UserName"]
        #linecount+=1
        print("One")

    print(KillDeath, WinLoss, Names, Time)```

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Why do you want to do this with two loops instead of just one? – Manuel Apr 14 '21 at 16:09
  • Because one is to initialize the lists and the other is to fill the lists with the contents within the csv reader but now that you mention it i could make it just one loop. Its just that the two pieces of code where written at two different times and I didn't really think about simplifying it because I thought I was most likely going to break it becasue I have a very small idea of what I am actually doing. Though I try to solve issues as I get them through google. – NamelessNamed Apr 15 '21 at 01:43

1 Answers1

1

csv_reader is an iterator, so once you iterate over it the element are used. You can save the elements in a list and iterate over that as many times as you need:

input("nothing")

with open ("Rainbow Six Siege Survey.csv") as csv_file:
    csv_reader = csv.DictReader(csv_file)
    content = list(csv_reader)

    line_count = 0

    WinLoss = []
    KillDeath = []
    Names = []
    Time = []

    for i in range(0, len(content)):
        WinLoss.append(0)
        KillDeath.append(0)
        Names.append("")
        Time.append("")


    for row in content:
        #if line_count == 0:
            #print("The columns are: {",".join(row)}")
        #else:
            #Names[line_count-1] = row["UserName"]
        #linecount+=1
        print("One")

    print(KillDeath, WinLoss, Names, Time)
Pietro
  • 1,090
  • 2
  • 9
  • 15
  • So the cvs reader expends itself after iterating through it once? That seems kinda odd but that would make sense why my code wouldn't execute I will try to implement your solution to see if it works – NamelessNamed Apr 15 '21 at 01:07
  • It worked after implementing it. Though I do want to understand what the list function does because I looked it up to be sure of its function but I am not quite sure what it does because the examples the internet gave me were not very clear. – NamelessNamed Apr 15 '21 at 01:15
  • Here is an explanation of what [iterators](https://www.programiz.com/python-programming/iterator) and [generators](https://www.programiz.com/python-programming/generator) are. The main idea is that the object you iterate over is not all in memory (which for a 1Gb csv file would be counterproductive), but the single element you need in that iteration is generated as needed. For the file, each line is read and parsed at each iteration, which allows for low memory usage and fast results, as you do not need to wait for the whole file to be parsed. – Pietro Apr 15 '21 at 07:20
  • Yes @Manuel, an iterator is a class that implements `__iter__` and `__next__`, while a generator is a function with a `yield` statement inside. From a user point of view, the two are identical, and you can simply use the elements are they are provided in a `for element in something:` loop. – Pietro Apr 15 '21 at 07:57