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 [email protected]?
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"
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.
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 [email protected]?
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"
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.
Share Improve this question edited Mar 13 at 3:09 nicholas asked Mar 13 at 3:00 nicholasnicholas 2,78816 gold badges67 silver badges118 bronze badges 4 |1 Answer
Reset to default 1trivial 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"]
w.writerow( email )
f.close()
~
python nic.py
cat /tmp/nic.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
.writerow()
expects a sequence. You give it a string, which is a sequence of characters. Put your string in a list or tuple, i.e.[email]
– Grismar Commented Mar 13 at 3:21csvwriter.writerow([email])
. When you pass a string, csv.writer treats each character as a separate element and writes them as individual columns. – Lewis Commented Mar 13 at 4:40