我有以下表格的数据:
{'count': '274', 'file_type': 'json', 'limit': '100000', observation_end': '9999-12-31', 'observation_start': '1776-07-04', 'observations': "[{'date': '1947-01-01', 'realtime_start': '2015-08-20', 'realtime_end': '2015-08-20', 'value': '3.962'}, {'date': '1947-04-01', 'realtime_start': '2015-08-20', 'realtime_end': '2015-08-20', 'value': '4.052'}, {'date': '1947-07-01', 'realtime_start': '2015-08-20', 'realtime_end': '2015-08-20', 'value': '4.172'}, {'date': '1947-10-01', 'realtime_start': '2015-08-20', 'realtime_end': '2015-08-20', 'value': '4.270'}, {'date': '1948-01-01', 'realtime_start': '2015-08-20', 'realtime_end': '2015-08-20', 'value': '4.372'}, {'date': '1948-04-01', 'realtime_start': '2015-08-20', 'realtime_end': '2015-08-20', 'value': '4.432'}, {'date': '1948-07-01', 'realtime_start': '2015-08-20', 'realtime_end': '2015-08-20', 'value': '4.521'}]
我想从中创建CSV或Excel文件,以便在不同的行上显示不同的值。例如数据1948-07-01价值4521.是否可能以及如何? 我试过以下:
writer = csv.writer(open('dict.csv', 'wb'))
for key, value in mydict.items():
writer.writerow([key, value])
但最后一行包含单行列表中的所有词典。
答案 0 :(得分:1)
import ast # since your observations seems to be a string
a=your_dictionary
observations=ast.literal_eval(a["observations"]) # this will convert this string to list
headers=observations[0].keys()
writer = csv.writer(open('dict.csv', 'wb'))
writer.writerow(headers) # to make sure your headings are date,realtime_start etc
for data in observations:
row=[data[header] for header in headers] #will create the row in same order as headers
writer.writerow(row)
答案 1 :(得分:0)
您必须首先使用键编写标题,然后编写您的值。
鉴于您的所有数据都具有相同的密钥,以下内容将起作用:
collections.OrderedDict
请务必使用.keys
确保.values
和using UnityEngine;
using System.Collections;
public class LoadingScreen : MonoBehaviour {
public string levelToLoad;
public GameObject background;
public GameObject text;
public GameObject progressBar;
private int loadProgress = 0;
// Use this for initialization
void Start () {
background.SetActive (false);
text.SetActive (false);
progressBar.SetActive (false);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("space")) {
StartCoroutine(DisplayLoadingScreen(levelToLoad));
}
}
IEnumerator DisplayLoadingScreen(string level){
background.SetActive (true);
text.SetActive (true);
progressBar.SetActive (true);
progressBar.transform.localScale = new Vector3(loadProgress,progressBar.transform.localScale.y, progressBar.transform.localScale.z);
text.GetComponent<GUIText>().text = "Loading Progress " + loadProgress + "%";
AsyncOperation async = Application.LoadLevelAsync (level);
while (!async.isDone) {
loadProgress = (int)(async.progress * 100);
text.GetComponent<GUIText>().text = "Loading Progress " + loadProgress + "%";
progressBar.transform.localScale = new Vector3(async.progress,progressBar.transform.localScale.y, progressBar.transform.localScale.z);
yield return null;
}
}
方法以相同的顺序返回项目。