Python 写入和读取txt数据
写数据到txt里
一. 在本地盘符里新建一个txt文本文档,如下图所示
二.打开PyCharm,新建一个Python File ,写入以下代码
import random
import string
#写数据到txt文件里
all_str = string.ascii_letters + string.digits
for i in range(1,11):
account = ''.join(random.sample(all_str,8))+'@163.com'
password = random.randint(100000,999999)
x = str(account) +"," + str(password) + "\n"
with open("E:\\JMETER\\account.txt","a") as yy:
yy.write(x)
print(u"生成第[%d]个账号"%(i))
三.运行程序,查看结果
四.检查盘符的txt文档数据
读取txt文本文档里的数据
一.打开txt文本文档
二.打开PyCharm,创建一个Python File ,写入如下所示代码
#读取txt文件
file=open("E:\\JMETER\\account.txt",'r',encoding='utf-8')
userlines = file.readlines()
file.close()
for line in userlines:
account = line.split(',')[0]
password= line.split(',')[1]
print(account,password)
三.运行程序,查看结果
四.检查程序结果和txt文本文档数据