Last active
February 19, 2025 10:17
-
-
Save manesec/08a8d0d2124cfba58344b35cf04c5690 to your computer and use it in GitHub Desktop.
隨機生成 名字,職位,身份證,手機號碼,安全郵箱,地址,基本銀行賬號,MAC地址 并保存成 csv。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from faker import Faker | |
| import csv | |
| # 初始化 Faker,設定本地化為中國 | |
| fake = Faker('zh_CN') | |
| # CSV 檔案名 | |
| csv_file = 'sensitive.csv' | |
| # 欄位名稱 (對應到使用者要求的字段) | |
| fieldnames = ['名字', '職位', '身份證', '手機號碼', '安全郵箱', '地址', '基本銀行賬號', 'MAC地址'] | |
| # 生成 5000 行記錄 | |
| records = [] | |
| for _ in range(5000): | |
| profile = fake.simple_profile() # 使用 simple_profile 获取一些基本信息作为起点 | |
| records.append({ | |
| '名字': fake.name(), | |
| '職位': fake.job(), | |
| '身份證': fake.ssn(), # 使用 ssn() 在 zh_CN locale 下生成身份證號碼 | |
| '手機號碼': fake.phone_number(), | |
| '安全郵箱': fake.safe_email(), | |
| '地址': fake.address(), | |
| '基本銀行賬號': fake.bban(), # bban() 在 zh_CN locale 下生成銀行帳號格式 | |
| 'MAC地址': fake.mac_address() | |
| }) | |
| # 寫入 CSV 文件 | |
| with open(csv_file, 'w', newline='', encoding='utf-8') as csvfile: | |
| writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
| writer.writeheader() # 寫入 header | |
| writer.writerows(records) # 寫入所有記錄 | |
| print(f"已生成 {len(records)} 行記錄並保存到 {csv_file} 文件。") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment