414373 (4) [Avatar] Offline
#1
Hi Ana,

I am reviewing 'continue' statement in section 18.3.2 of the book. I am trying to relate "None" and "Continue" operations with the following code snippet. Please confirm my visualization of how the for-loop works, and advise how I can determine the length of outputs.

My pseudo code for the function:
Local variable headers has, by-default, absence of value. So once the 1st line has been assigned to headers in 1st iteration, headers should have 1st line, 1960 ~ 1962, as values. In the 2nd iteration, since the if-condition has been satisfied, the 2nd line should be assigned to row and the continue statement will re-direction the execution of the for-loop to beginning of the loop for the next iterable. In subsequent iteration, all remaining lines will be assigned to row.

def csv_printHeaderRows(csv_file):
    headers = None
    for line in csv_file:  # 5 iteration
        row = line.rstrip().split(',')
        if headers is None:
            headers = row
            continue
        print(headers, row)


Actual output:
csv_printHeaderRows(open(in_file_1))

['1960', '1961', '1962'] ['54211', '55438', '56225'] # 1st iteration
['1960', '1961', '1962'] ['8996351', '9166764', '9345868'] # 2nd iteration
['1960', '1961', '1962'] ['5643182', '5753024', '5866061'] # 3rd iteration
['1960', '1961', '1962'] ['1608800', '1659800', '1711319'] # 4th iteration
['1960', '1961', '1962'] ['13411', '14375', '15370'] # 5th iteration


My visualization of how the loop works:
['1960', '1961', '1962'] # 1st iteration --> headers
['54211', '55438', '56225'] # 2nd iteration --> row due to continue statement
['8996351', '9166764', '9345868'] # 3rd iteration --> row due to continue statement
['5643182', '5753024', '5866061'] # 4th iteration --> row due to continue statement
['1608800', '1659800', '1711319'] # 5th iteration --> row due to continue statement
['13411', '14375', '15370'] # 6th iteration --> row due to continue statement. Iterable now expended.

The csv file looks like this:
in_file_1.head()

Out[55]:
1960 1961 1962
0 54211.0 55438.0 56225.0
1 8996351.0 9166764.0 9345868.0
2 5643182.0 5753024.0 5866061.0
3 1608800.0 1659800.0 1711319.0
4 13411.0 14375.0 15370.0

I am unable to check the length of headers and row variables. I am not able to modify the control structure in the function properly to accumulate the outputs to headers and row. e.g., I am expecting headers variable to contain only a list of 3 elements, and row to contain 5 lists of 3 elements each.

headers = None
rows = []
for line in open(in_file_1):
    row = line.rstrip().split(',')
    if headers is None:
        headers = row
        continue
    rows.append(row)



In[20]: headers
Out[20]: ['1960', '1961', '1962'] # how to accumulate possible append to headers???
In[21]: row
Out[21]: ['13411', '14375', '15370'] # last line in csv file only !!!
Ana Bell (33) [Avatar] Offline
#2
I think the code you wrote is correct.

Using this data file:

1960,1961,1962 
54211.0,55438.0,56225.0 
8996351.0,9166764.0,9345868.0 
5643182.0,5753024.0,5866061.0 
1608800.0,1659800.0,1711319.0 
13411.0,14375.0,15370.0


This is what I think you have, but I added two print statements inside the function:

def csv_printHeaderRows(csv_file):
    headers = None
    rows = []
    for line in csv_file:  # 5 iteration
        row = line.rstrip().split(',')
        if headers is None:
            headers = row
            continue
        rows.append(row)
    print(headers)
    print(rows)
    
csv_printHeaderRows(open("temp.txt"))


The print statements show me that headers is the list:

['1960', '1961', '1962']


And that rows (not row) is the list of lists, where each sublist is a row:

[['54211.0', '55438.0', '56225.0'], ['8996351.0', '9166764.0', '9345868.0'], ['5643182.0', '5753024.0', '5866061.0'], ['1608800.0', '1659800.0', '1711319.0'], ['13411.0', '14375.0', '15370.0']]


Am I misunderstanding your question?
414373 (4) [Avatar] Offline
#3
Hi Ana,

Merry Christmas and a Happy New Year to You smilie

Yes, you understood my query, made the outputs explicit to me, and confirm my understanding of how the control structure works. I did not notice the bug when I work on this code snippet near mid-night; I called row instead of rows.

Best Regards
Daniel