Python Forum
How to output one value per request of the CSV and print it in another func?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to output one value per request of the CSV and print it in another func?
#1
Can You please tell me how to do. The first function needs to read USERNAME column from csv file to output one value per request and the second function outputs this value to the console with 5 seconds pause. To cycle through the entire column in the file. Thanks!!
import csv
import time

filename = "D:/Study/file/.vscode/telescript/DarkCryptoMining.csv"

def func1():
    global Field  
    with open(filename, 'r', encoding="utf-8", newline='') as f:
         data = csv.DictReader(f, delimiter=";")
         for str in data:


          for row in data:
             if row['USERNAME'] == "-":
                time.sleep(0)
             else:
                Field = (f'{row["USERNAME"]}')
                
def func2():
   time.sleep(5)
   print(Field)
                
def main():
   func1() 
   func2()               


main()
snippsat write Nov-11-2022, 04:49 PM:
Fixed code tag in your post,look at BBCode on how to use.
Reply
#2
The main body of your program is going to have a loop where you call func1, which returns a value from the username column, then calls func2 which prints the returned username value and waits 5 seconds.

Do not use global variables to pass information between your functions. Use function arguments to pass a value to a function and use "return value" to return a value from a function.

The tricky part is how to open the CSV file and get the usernames one at a time. Your current solution will not work because each time you call func1 you open the file and return the first name. Are you familiar with generators and the "yield" statement? My guess is yes, since the assignment seems designed to force using a generator.
Reply
#3
It's better not to use reserved words when declaring variables, such as for str in data:, because if you need to use the str() function in that loop, you won't be able to.

Such usage can lead to confusion if you've used the likes of str = "Whatever" at some point and need the function further down; an error such as [builtins.TypeError: 'str' object is not callable] could very well throw you for a while, until you figure out what you've done: code blindness sets in, along with some frustration, if you fail to spot the mistake right away -- prevention is better than a cure.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
To show with some code what deanhystad talks about.
No global💀 see how arguments are pass to functions.
Your Field varible will never work as will as will only get last execution in loop assigned.
Here wilh yield.
test.csv:
Output:
one,two,three 1,2,3 3,4,5
import csv
import time

def func1(filename):  
    with open(filename, encoding="utf-8", newline='') as f:
        reader = csv.reader(f, delimiter=",")
        for row in reader:
            #print(row[0])
            yield row[0]    

def func2(field):
    time.sleep(5)
    #print(list(field))
    for row in field:
        print(row)

if __name__ == '__main__':
    filename = "test.csv"
    result = func1(filename)
    func2(result)
Output:
one 1 3
Here with a list and append not as elegant as yield in this case.
But you see how this can be used to store result that's done in loop,just a variable in loop will overwrite all but last result.
import csv
import time

def func1(filename):
    field = []
    with open(filename, encoding="utf-8", newline='') as f:
        reader = csv.reader(f, delimiter=",")
        for row in reader:
            #print(row[0])
            field.append(row[0])
    return field      

def func2(field):
    time.sleep(5)    
    #print(field)
    for row in field:
        print(row)

if __name__ == '__main__':
    filename = "test.csv"
    result = func1(filename)
    func2(result)
Output:
one 1 3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,109 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to print the output of a defined function bshoushtarian 4 1,323 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 10,999 Jan-11-2022, 07:04 PM
Last Post: snippsat
Photo print output none 3lnyn0 4 1,849 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  how can I correct the Bad Request error on my curl request tomtom 8 5,091 Oct-03-2021, 06:32 AM
Last Post: tomtom
  output correction using print() function afefDXCTN 3 11,119 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,934 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  Getting a GET request output text into a variable to work with it. LeoT 2 3,041 Feb-24-2021, 02:05 PM
Last Post: LeoT
  print function output wrong with strings. mposwal 5 3,142 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 4,073 Jan-16-2021, 09:42 AM
Last Post: xninhox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020