最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Write CSV file with data in new row in python - Stack Overflow

programmeradmin0浏览0评论

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
  • 3 .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:21
  • Pass email as a list containing a single string like this csvwriter.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
  • 3 If there’s only one entry per line, you don’t really need CSV to begin with, just write a line into a text file. Might want to ensure the email itself doesn’t contain line breaks, but that’s it. – deceze Commented Mar 13 at 6:33
  • this should do the trick """email=[''.join(random.choices(string.ascii_letters + string.digits, k=10)) + str( x + 1) + "@gmail"] """ , example posted – ticktalk Commented Mar 14 at 0:21
Add a comment  | 

1 Answer 1

Reset to default 1

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"]
     w.writerow( email )

f.close()

~         

python nic.py
cat /tmp/nic.txt 
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
发布评论

评论列表(0)

  1. 暂无评论