我如何在python中将json转换为csv

时间:2015-11-20 16:12:31

标签: python json csv

如何将以下json格式日志转换为csv中的python文件:
Json输入(log.json):

{"time":"14472","abc":"0","Def":[{"name":"C","value":77},{"name":"N","value":88}]}  
{"time":"1447","abc":"1","Def":[{"name":"C","value":99,{"name":"N","value":0.12}]}  

Csv输出:

time   abc    name   value  
14472   0      C      77  
14472   0      N      88  
1447    1      C     99    
1447    1      N      0.12  

2 个答案:

答案 0 :(得分:0)

查看https://docs.python.org/2/library/json.html

我没有亲自使用它,但是我浏览了一次文档,似乎它为每个json对象创建了一个字典,并为每个数组创建了一个列表。

一旦你有了那个,那么你可以得到列标签的密钥,然后你可以实际写文件不是什么大事。

答案 1 :(得分:0)

这应该假设' log.json'输入文件包含有效的json对象:

    [TestMethod]
    public void SequenceActivityCompile()
    {



        Activity sequence = new Sequence
        {
            Activities = { new CSharpValue<string>("\"Hello World \"") }
        };


        CompileExpressions(sequence);
        var result = WorkflowInvoker.Invoke(sequence);


    }



    [TestMethod]
    public void CodeActivityCompile()
    {

        var code = new CSharpValue<String>("\"Hello World\"");
        CompileExpressions(code);
        var result = WorkflowInvoker.Invoke(code);


    }


    void CompileExpressions(Activity activity)
    {
        // activityName is the Namespace.Type of the activity that contains the
        // C# expressions.
        string activityName = activity.GetType().ToString();

        // Split activityName into Namespace and Type.Append _CompiledExpressionRoot to the type name
        // to represent the new type that represents the compiled expressions.
        // Take everything after the last . for the type name.
        //string activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
        string activityType = "TestType";
        // Take everything before the last . for the namespace.
        //string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
        string activityNamespace = "TestSpace";

        // Create a TextExpressionCompilerSettings.
        TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
        {
            Activity = activity,
            Language = "C#",
            ActivityName = activityType,
            ActivityNamespace = activityNamespace,
            RootNamespace = null,
            GenerateAsPartialClass = false,
            AlwaysGenerateSource = true,
            ForImplementation = false
        };

        // Compile the C# expression.
        TextExpressionCompilerResults results =
            new TextExpressionCompiler(settings).Compile();

        // Any compilation errors are contained in the CompilerMessages.
        if (results.HasErrors)
        {
            throw new Exception("Compilation failed.");
        }

        // Create an instance of the new compiled expression type.
        ICompiledExpressionRoot compiledExpressionRoot =
            Activator.CreateInstance(results.ResultType,
                new object[] { activity }) as ICompiledExpressionRoot;

        // Attach it to the activity.
        System.Activities.Expressions.CompiledExpressionInvoker.SetCompiledExpressionRoot(
            activity, compiledExpressionRoot);
    }