简体   繁体   中英

How to insert function after printing list from csv file?

I printed a specific list from csv file and tried to add a function after the print.

If I do not add the function, the list will be printed in whole and accurately.

If I add the function, only the first line of the list is printed but the function is still able to run after the incomplete list is printed.

I tried to use line separator but it doesn't work as intended.

for line in menu_csv_reader: #looks into menu file
            print(line[0]) #print menu
            Back()

Output: - Incomplete list (only first line printed) - Back() function able to run


for line in menu_csv_reader: #looks into menu file
            print(line[0]) #print menu
            print('\n')
            print('\n')
            print('\n')
            print('\n')
            Back()

Output: - Incomplete list (only first line printed) - Then skips 4 lines - Then Back() function able to run


WITHOUT BACK() FUNCTION

for line in menu_csv_reader: #looks into menu file
            print(line[0]) #print menu

Output: - Complete list


WITHOUT BACK() FUNCTION

for line in menu_csv_reader: #looks into menu file
            print(line[0]) #print menu
            print('\n')
            print('\n')
            print('\n')
            print('\n')

Output: - Complete list - 4 lines skipped per entry from list;

eg: A

B

C


Expected result:

for line in menu_csv_reader: #looks into menu file
            print(line[0]) #print menu
            Back()

Output: - Complete list printed - Back() function able to run after complete list is printed

If you want the back function to be run after the list is printed, it cannot be indented into the loop. Make the indentation match the for statement and it will run after the list is printed.

Remove the indent for the function if you want the function to run after the complete list has been printed.

With the function inside the iteration it is trying run after each line print.

With the function inside when adding in the four line breaks only means it prints the first line and the four line breaks then tries to run the function.

When the function is inside the loop, it executes for as many times as the loop iterates. Put the function outside it, and it will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM