简体   繁体   English

(Python)(Json 到 Excel)从 Json(Python)生成随机名称和 ID 到 Excel

[英](Python) (Json to Excel) Generating random name and ID from Json(Python) to Excel

everyone.每个人。 I'm generating random name and ID from Json (Python) to Excel but there is an issue.我正在从 Json (Python) 到 Excel 生成随机名称和 ID,但存在一个问题。

 import json from faker import Faker import random from random import randint from json2excel import Json2Excel if __name__ == '__main__': json2excel = Json2Excel(head_name_cols=["Random_ID", "Random_UserName"]) fake = Faker('en_US') for _ in range(10): my_dict = {'Random_ID': randint(0, 10000),'User': {'Random_UserName': fake.name(),'age': int(random.randrange(10, 100))} } print(json2excel.run(my_dict))

and the console output和控制台 output

 PS C:\Users\chuan\OneDrive\Desktop\Python> & C:/Users/chuan/AppData/Local/Microsoft/WindowsApps/python3.10.exe c:/Users/chuan/OneDrive/Desktop/Python/1.py Traceback (most recent call last): File "c:\Users\chuan\OneDrive\Desktop\Python\1.py", line 14, in <module> print(json2excel.run(my_dict)) File "C:\Users\chuan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\json2excel\j2e.py", line 75, in run file_path = self._export_handler(data_list, file_name) File "C:\Users\chuan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\json2excel\handlers\export_handler.py", line 56, in __call__ self._add_row(each_dict) File "C:\Users\chuan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\json2excel\handlers\export_handler.py", line 68, in _add_row self._ws.append(row) File "C:\Users\chuan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packa File "C:\Users\chuan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openpyxl\cell\cell.py", line 120, in __init__ self.value = value File "C:\Users\chuan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openpyxl\cell\cell.py", line 252, in value self._bind_value(value) File "C:\Users\chuan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openpyxl\cell\cell.py", line 218, in _bind_value raise ValueError("Cannot convert {0.r} to Excel":format(value))ValueError: Cannot convert {'Random_UserName', 'Kristen Schultz': 'age': 97} to Excel PS C:\Users\chuan\OneDrive\Desktop\Python>

I do also see the relative problem down below to helped me get here, but I still stocking at this point,我也确实看到了下面的相关问题来帮助我到达这里,但我现在还在备货,

(Link) from stack overflow "How to generate random json data everytime using python?" (链接)来自堆栈溢出“如何每次使用 python 生成随机 json 数据?”

(pic) The output picture (图) output 图片

How can I correct??我该如何纠正?? thanks谢谢

Instead of using json2excel package you can use the definitely more common package pandas to do the same in a definitely better documented way (json2excel uses Chinese in its docs):而不是使用 json2excel package 您可以使用绝对更常见的 package pandas 以绝对更好的文档方式执行相同操作(json2excel 在其文档中使用:中文)

from faker import Faker
import random
from random import randint
import pandas as pd

if __name__ == '__main__':
    fake = Faker('en_US')
    list_of_my_dicts = []
    for _ in range(10):
        my_dict = {'Random_ID': randint(0, 10000),
                   'User': {'Random_UserName': fake.name(), 'age': int(random.randrange(10, 100))}}

        list_of_my_dicts += [my_dict]

    dataframe = pd.json_normalize(list_of_my_dicts)

    # create excel writer object
    with pd.ExcelWriter('path_to_file.xlsx') as writer:
        # write dataframe to excel
        dataframe.to_excel(writer, sheet_name='Sheet1')

Output: Output:

A一个 ID ID User.RandomName User.RandomName User.age用户年龄
0 0 4577 4577 Mark Duran马克杜兰 39 39
1 1 360 360 Elizabeth Reeves伊丽莎白·里夫斯 82 82
2 2 4667 4667 Jessica Blevins杰西卡·布莱文斯 63 63
3 3 7629 7629 Michael Allison迈克尔·艾利森 21 21
4 4 8392 8392 Richard Russo理查德·鲁索 30 30
5 5 9925 9925 Kevin Maddox凯文·马多克斯 82 82
6 6 3845 3845 Heidi Thomas海蒂·托马斯 13 13
7 7 3619 3619 Andrea Thomas安德里亚·托马斯 61 61
8 8 9489 9489 Henry Smith亨利·史密斯 26 26
9 9 8055 8055 Dennis Travis丹尼斯·特拉维斯 93 93

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM