我想使用csv以这种格式将列表的字典写到csv文件中:
Ola, elizabethy, beale/cambray,st_hildas
Evie, st_hildas,beale/cambray,elizabethn
Lorna, beale/cambray,elizabethn,st_hildas
这样我最终可以将其写入如下所示的excel文件中:
Ola elizabethy beale/cambray st_hildas
Evie st_hildas beale/cambray elizabethn
Lorna beale/cambray elizabethn st_hildas
这是它在python中的外观。
students = {"Ola":["elizabethy","beale/cambray","st_hildas"],"Evie":["st_hildas","beale/cambray","elizabethn"],"Lorna":["beale/cambray","elizabethn","st_hildas"],"Morgan":["st_hildas","beale/cambray","elizabethy"],"Ola1":["beale/cambray","elizabethn","st_hildas"],"Lorna1":["elizabethn","beale/cambray","st_hildas"],"Lorna2":["beale/cambray","elizabethn","st_hildas"],"Evie1":["st_hildas","beale/cambray","elizabethy"],"Evie3":["st_hildas","beale/cambray","elizabethn"]}
答案 0 :(得分:0)
d$X <- unlist(lapply(1:nrow(d), function(x){
ifelse(is.na(d[x,]$X), d[x-1,]$X, d[x,]$X)
}
))
答案 1 :(得分:0)
要写入CSV文件(不导入任何库)
students = {
"Ola":["elizabethy","beale/cambray","st_hildas"],
"Evie":["st_hildas","beale/cambray","elizabethn"],
"Lorna":["beale/cambray","elizabethn","st_hildas"],
"Morgan":["st_hildas","beale/cambray","elizabethy"],
"Ola1":["beale/cambray","elizabethn","st_hildas"],
"Lorna1":["elizabethn","beale/cambray","st_hildas"],
"Lorna2":["beale/cambray","elizabethn","st_hildas"],
"Evie1":["st_hildas","beale/cambray","elizabethy"],
"Evie3":["st_hildas","beale/cambray","elizabethn"]}
with open('test.csv', 'w') as f:
for key in students.keys():
f.write("%s, %s\n"%(key,','.join(students[key])))
要将CSV重新读回字典中:
students1 = dict()
with open('test.csv', 'r') as f:
for line in f:
key, value = line.strip().split(', ')
students1[key] = value.split(',')
答案 2 :(得分:0)
from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.platypus.flowables import TopPadder
from reportlab.platypus import Table, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
import numpy as np
document = SimpleDocTemplate('padding_test.pdf')
table = Table(np.random.rand(2,2).tolist(),
style=[('GRID', (0, 0), (-1, -1), 0.5, colors.black)])
styles = getSampleStyleSheet()
paragraph = Paragraph('Some paragraphs', style=styles['Normal'])
document.build([
paragraph,
TopPadder(table),
])
# This should generate a single pdf page with text at the top and a table at the bottom.