我试图让Python将5行文本写入CSV文件,但要将单个值重写到旁边的单元格中,而不是下面的单元格中。
目前,程序能够写入所有变量,值,因为它想要的,但没有适当的安排。但是,csv.write和csv.writerows不断覆盖从单元格,使其增加值到单元格[A1],然后[A2],而不是[A1],然后[B1]。有没有一个函数,我可以使用,写一切就像例如打印(f”{variable}",end =',')
我对Python很陌生,对不起。
文件名= 'test1.csv'
打开(文件名,模式=“w”)作为文件:writer = csv.编写器(文件)
with open('1.json', 'r', encoding='utf-8') as f:
data = json.load(f)
categories = data['categories']
writer.writerow(f"URL,{','.join(categories)}")
for file in json_files:
with open(file, 'r', encoding='utf-8') as f:
data = json.load(f)
writer.writerow(url)
for category in data['categories']:
scores = data['categories'][category]['score']
writer.writerow(f"{scores}")
**# Desired output:
#
# ,
#
# URL,performance,accessibility,best-practices,seo,pwa,visibility,security,ads,mobile
# URL, 0.23, 0.56, 0.57, 0.67, 0.31, 0.67, 0.37, 0.54, 0.71,
# URL, 0.53, 0.53, 0.57, 0.8, 0.23, 0.67, 0.5, 0.62, 0.71,
# URL, 1, 1, 0.86, 0.88, 0.58, 0.67, 0.42, 0.38, 1,
#
#
#
# What ive got
#
# U,R,L,",",p,e,r,f,o,r,m,a,n,c,e,",",a,c,c,e,s,s,i,b,i,l,i,t,y,",",b,e,s,t,-,p,r,a,c,t,i,c,e,s,",",s,e,o,",",p,w,a,",",v,i,s,i,b,i,l,i,t,y,",",s,e,c,u,r,i,t,y,",",a,d,s,",",m,o,b,i,l,e
#
# URL
#
# 0,.,2,3
#
# 0,.,5,6
#
# 0,.,5,7
#
# 0,.,6,7
#
# 0,.,3,1
#
# 0,.,6,7
#
# 0,.,3,7
#
# 0,.,5,4
#
# 0,.,7,1
#
# URL
#
# 0,.,5,3
#
# 0,.,5,3
#
# 0,.,5,7
#
# 0,.,8
#
# 0,.,2,3
#
# 0,.,6,7
#
# 0,.,5
#
# 0,.,6,2
#
# 0,.,7,1
#
# URL
#
# 1
#
# 1
#
# 0,.,8,6
#
# 0,.,8,8
#
# 0,.,5,8
#
# 0,.,6,7
#
# 0,.,4,2
#
# 0,.,3,8
#
# 1
**
1条答案
按热度按时间xwbd5t1u1#
我假设
json
文件的结构如下(每个json
文件只有一个JSON对象):我想出了这个密码:
我创建了两个虚拟的
json
文件:1.json
2.json
然后创建以下
output.csv
: