• Faker 安装与介绍


    from faker import Faker
    import pandas as pd
    from urllib import parse
    from pymongo import MongoClient
    import pymysql


    class Create_Data(object):
    def __init__(self):
    # 选择中文
    fake = Faker('zh_CN')
    # 生成数据改变循环体来控制数据量rang(?)
    self.data_total = [
    [fake.name(), fake.job(), fake.company(), fake.phone_number(), fake.company_email(), fake.address(),
    fake.date_time(tzinfo=None)] for x in range(100)]
    print(self.data_total)

    # 写入excel
    def deal_excel(self):
    df = pd.DataFrame(self.data_total,
    columns=['name', 'job', 'company', 'phone_number', 'company_email', 'address', 'date_time'])
    # 保存到本地excel
    df.to_excel("data_total1.xlsx", index=False)
    print("Processing completed to excel")

    # 写入txt
    def deal_txt(self):
    with open('data_total.txt', 'w', errors='ignore', encoding='utf-8') as output:
    output.write('name,job,company,phone_number.company_email.address.date_time ')
    for row in self.data_total:
    rowtxt = '{},{},{},{}'.format(row[0], row[1], row[2], row[3])
    output.write(rowtxt)
    output.write(' ')
    output.close()
    print("Processing completed to txt")

    # 写入mongodb
    def deal_mongodb(self):
    port = 27017
    host = 'localhost'
    user_name = 'root'
    db_name = 'data'
    passwd = 'root'
    passwd = parse.quote(passwd)
    mango_uri = 'mongodb://%s:%s@%s:%s/%s' % (user_name, passwd, host, port, db_name) # 链接时需要指定数据库
    conn = MongoClient(mango_uri) # 创建链接
    db = conn[db_name] # 连接coder数据库
    mongodata = db['data_total']
    for val in self.data_total:
    mongodata.insert(
    {"name": val[0], 'job': val[1], 'company': val[2], 'phone_number': val[3], "company_email": val[4],
    'address': val[5], 'date_time': val[6]})
    print("Processing completed to mongodb")

    # 写入mysql
    def deal_mysql(self):
    # 打开数据库连接
    db = pymysql.connect("localhost", "root", "root", "test2")
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    # SQL 插入语句
    for val in self.data_total:
    sql = "insert into data_total(name,job,company,phone_number,company_email,address,date_time)value ('%s','%s','%s','%s','%s','%s','%s')" % (
    val[0], val[1], val[2], val[3], val[4], val[5], val[6])
    try:
    # 执行sql语句
    cursor.execute(sql)
    # 执行sql语句
    db.commit()
    print("insert ok")
    except:
    # 发生错误时回滚
    db.rollback()
    # 关闭数据库连接


    if __name__ == '__main__':
    data = Create_Data()
    data.deal_excel()
    data.deal_txt()
    data.deal_mongodb()
    data.deal_mysql()
  • 相关阅读:
    7月23日 R进行层次聚类算法的继续完善
    明天学习的内容
    MySql快速导出为excel文件
    数据挖掘的隐私边界 【转自《中欧商业评论》】
    【转】Data truncation: Truncated incorrect DOUBLE value:Mysql Update时
    7月20日下一步工作
    R对term进行kmeans聚类完整实例(tm包)
    数学之美 系列一 统计语言模型
    7月30日总结
    MathType输入Support 自动分开的问题
  • 原文地址:https://www.cnblogs.com/jiaoliuxuexi/p/15020433.html
Copyright © 2020-2023  润新知