我正在尝试找出如何将以下列表导出到.csv文件中,并与某些列匹配的情况。
[{'amount': '100', 'unit': 'g.', 'ingredient': 'mælkechokolade'}, {'amount': '20', 'unit': 'g.', 'ingredient': 'mini marshmallows'}, {'amount': '40', 'unit': 'g.', 'ingredient': 'saltede peanuts'}]
以上是通过字符串解析并使用正则表达式将内容与其正确的标头匹配的结果。
r = re.compile(r"(?P<amount>\d+)\s+(?P<unit>\w+.)\s+(?P<ingredient>.+?(?=<))")
print([m.groupdict() for m in r.finditer(s)])
是否可以使用.writerow正确导出该列表?到目前为止,我无法使其正常工作。
答案 0 :(得分:0)
您可以使用pandas.to_csv
来获取数据结构并将其写入csv:
{{ item["h_" + String(hour).padStart(2, '0')] }}
尽管需要import pandas as pd
somedata = [{'amount': '100', 'unit': 'g.', 'ingredient': 'mælkechokolade'}, {'amount': '20', 'unit': 'g.', 'ingredient': 'mini marshmallows'}, {'amount': '40', 'unit': 'g.', 'ingredient': 'saltede peanuts'}]
df = pd.DataFrame(somedata)
with open("somefile.csv", 'w') as fh:
df.to_csv(fh)
点安装(pandas
)。否则,您可以只使用内置的csv
模块:
pip install pandas
将会导致
import csv
somedata = [{'amount': '100', 'unit': 'g.', 'ingredient': 'mælkechokolade'}, {'amount': '20', 'unit': 'g.', 'ingredient': 'mini marshmallows'}, {'amount': '40', 'unit': 'g.', 'ingredient': 'saltede peanuts'}]
# This will keep order consistent
headers = [k for k in somedata[0].keys())
new_data = [[rec.get(header) for header in headers] for rec in somedata]
with open('somefile.csv', 'w') as fh:
writer = csv.writer(fh, delimiter=',')
writer.writerow(headers)
for row in new_data:
writer.writerow(row)
答案 1 :(得分:0)
您可以使用pandas DataFrame的to_csv函数
function animateVector3(vectorToAnimate, target, options) {
options = options || {}
// get targets from options or set to defaults
let to = target || new THREE.Vector3(),
easing = options.easing || TWEEN.Easing.Exponential.InOut,
duration = options.duration || 2000
// create the tween
let tweenVector3 = new TWEEN.Tween(vectorToAnimate)
.to({x: to.x, y: to.y, z: to.z}, duration)
.easing(easing)
.onStart(function(d) {
if (options.start) {
options.start(d)
}
})
.onUpdate(function(d) {
if (options.update) {
options.update(d)
}
})
.onComplete(function() {
if (options.finish) options.finish()
})
// start the tween
tweenVector3.start()
// return the tween in case we want to manipulate it later on
return tweenVector3
}
const animationOptions = {
duration: 2000,
start: () => {
this.cameraControls.enable(false)
},
finish: () => {
this.cameraControls.enable(true)
}
}
// Adjust Yaw object rotation
animateVector3(
// current rotation of the 3d object
yawObject.rotation,
// desired rotation of the object
new THREE.Vector3(0, 0, annotation.rotation.z + degToRad(90)),
animationOptions
)
// Adjust Pitch object rotation
animateVector3(
pitchObject.rotation,
new THREE.Vector3(0, degToRad(45), 0),
animationOptions
)