pythonpython-3.xlocust

Write CSV file with data in new row in python


I using python locust to do load test and my questions is i try to write csv file with email address as new row but it is separated the email charaters with ',' with each chracter like below:

z,b,r,X,S,F,i,E,p,H,1,@,g,m,a,i,l,.,c,o,m
I,q,O,2,i,t,a,Q,w,m,2,@,g,m,a,i,l,.,c,o,m
5,w,J,b,1,v,Y,X,f,H,3,@,g,m,a,i,l,.,c,o,m
G,J,S,y,I,Y,S,S,9,e,4,@,g,m,a,i,l,.,c,o,m
L,M,8,R,B,a,p,g,I,0,5,@,g,m,a,i,l,.,c,o,m

How to make it like LM8RBapgI05@gmail.com?

My code as below:

     @task
     def register(self):
            email: str
            telcoCode: int
            phone: int

            self.client.request_name = "API Register User"
            with open("email.csv", "w", newline="") as csvfile:
                for x in range(5):
                    email = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + str(
                        x + 1) + "@gmail.com"
                    telcocode = random.randint(10, 19)
                    phone = random.randint(10000000, 99999999)
                    phonestr = "+60" + str(telcocode) + str(phone)

                    csvwriter = csv.writer(csvfile)
                    csvwriter.writerow(email)
                    self.client.headers = {
                        "x-api-key": "KZThJSCH1vWj5snaeQQ+TQ5rTbucayxXtDVHFmfRSJag5gLkUKOMgrpitL1jHIVr6bYAN5N9NrKWg6RhdgNThQ=="}
                    response = self.client.post("https://",
                                                {"email": email, "phone": phonestr,
                                                 "password": "abcd1234", "password_confirmation": "abcd1234"}
                                                )
                    assert response.status_code == 200, "Failed to register, status code: " + str(response.status_code)
                    logging.info(response.text)

Please help. Thanks in advance.


Solution

  • trivial example - for demo purposes !

    cat nic.py
    import random
    import string
    import csv
    
    f = open("/tmp/nic.txt", "w")
    w = csv.writer(f)
    
    for x in range(5):
         email=[''.join(random.choices(string.ascii_letters + string.digits, k=10)) + str( x + 1) + "@gmail.com"]
         w.writerow( email )
    
    f.close()
    
    ~         
    
    python nic.py
    cat /tmp/nic.txt 
    WzFkuK4DLY1@gmail.com
    rqFiIbU2vh2@gmail.com
    dOj4aiewEo3@gmail.com
    ValSRNi4Q94@gmail.com
    V0gDckcy5q5@gmail.com