示例代码 1:
import pandas as pd
from pandas import DataFrame
def pandas_write_excel():
data = {
'name': [u'张三', u'李四', u'王五'],
'age': [21, 22, 23],
'sex': [u'男', u'女', u'男']
}
df = DataFrame(data)
df.to_excel('./pandas_write_excel.xlsx')
def pandas_read_excel():
sheet = pd.read_excel('./pandas_write_excel.xlsx')
for row in sheet.values:
print(row)
if __name__ == '__main__':
pandas_write_excel()
pandas_read_excel()
pass
示例代码 2:
test.json 文件
[
{
"_id": "11041",
"short_name": "tes_name_1",
"食品饮料、酒类、生鲜": 1.2,
"手机、数码": 1.0,
"家具、家具、家装、厨具": 0.83,
"家用电器": 0.8
},
{
"_id": "11037",
"short_name": "test_name_2",
"食品饮料、酒类、生鲜": 1.18,
"手机、数码": 1.0,
"家用电器": 0.76,
"家具、家具、家装、厨具": 0.74,
"电脑、办公": 0.6,
"个护化妆": 0.54,
"母婴、玩具乐器": 0.51,
"运动户外": 0.42,
"营养保健": 0.41
}
]
write_excel.py
import json
import pandas as pd
from pandas import DataFrame
def pandas_write_excel():
with open('./test.json', 'rb') as f:
data = json.load(f)
print(json.dumps(data, ensure_ascii=False, indent=4))
df = DataFrame(data)
df.to_excel('./test.xlsx')
pass
if __name__ == '__main__':
pandas_write_excel()
结果截图: