我有一个Json文件,我正在转换并附加到csv,我还试图添加一个月/日/年格式的日期列。
最近一次尝试:
import json
from csv import DictWriter
import pandas as pd
import datetime
with open('output.json', 'r') as inp, open('output.csv', 'a') as outp:
writer = DictWriter(outp, fieldnames=[
'name', 'price'])
for line in inp:
row = json.loads(line)
writer.writerow(row)
a = pd.read_csv('output.csv')
a['Date'] = [datetime.date.today()]*len(a)
a.to_csv('adate.csv')
我接近了我正在尝试做的事情。输出:
Lattice Top 6'W x 6'H King Cedar Aluminum/Composite Horizontal Fence-Starter Section-SURFACE MOUNT,"4,300 .",Date
"FENCESMART4U XCEL - Black Steel Anti-Rust Fence Gate - Sharp End Pickets - 4ft W x 5ft H - Easy Installation Kit, for Residential, Outdoor, Yard, Patio, Entry Way, 3-Rail Metal Gate",199 .,2022-10-18
"XCEL - Black Steel Anti-Rust Fence Gate - 4ft W x 5ft H - Easy Installation Kit, for Residential, Outdoor, Yard, Patio, Entry Way, 3-Rail Metal Gate",206 .,2022-10-18
"LZMZMQ Courtyard Baluster Garden Fencing Villa Outdoor Guardrail, Wide 100 120 140 160 180 cm Aluminum Fence Panels Deckorators, Grey Panels Decor Free Standing for Weatherproof","1,771 .",2022-10-18
我试过添加列标题,如headerList = ['name', 'price', 'date']
,但在第一列下效果不佳,因为它删除了产品名称。
这是json文件阅读的方式:
{"name": "Lattice Top 6'W x 6'H King Cedar Aluminum/Composite Horizontal Fence-Starter Section-SURFACE MOUNT", "price": "4,300 ."}
{"name": "FENCESMART4U XCEL - Black Steel Anti-Rust Fence Gate - Sharp End Pickets - 4ft W x 5ft H - Easy Installation Kit, for Residential, Outdoor, Yard, Patio, Entry Way, 3-Rail Metal Gate", "price": "199 ."}
{"name": "XCEL - Black Steel Anti-Rust Fence Gate - 4ft W x 5ft H - Easy Installation Kit, for Residential, Outdoor, Yard, Patio, Entry Way, 3-Rail Metal Gate", "price": "206 ."}
{"name": "LZMZMQ Courtyard Baluster Garden Fencing Villa Outdoor Guardrail, Wide 100 120 140 160 180 cm Aluminum Fence Panels Deckorators, Grey Panels Decor Free Standing for Weatherproof", "price": "1,771 ."}
3条答案
按热度按时间djp7away1#
您可以直接在
pandas
中完成这些操作:输出(针对示例数据):
ryevplcw2#
如果需要的话,你可以不使用
pandas
直接执行此操作。确保在使用csv
模块的文件上使用newline=''
。(为什么)输出量:
cbeh67ev3#
这个解决方案将利用参数
restval
,这意味着如果一个值丢失,DictWriter
可以用这个值来填充。所以我这里的代码和原始的海报代码几乎一样,但是使用了
restval
。