Converting string file into json format file

后端 未结 3 1095
别跟我提以往
别跟我提以往 2021-02-04 20:22

Ok , let say that I have a string text file named \"string.txt\" , and I want to convert it into a json text file. What I suppose to do? I have tried to use \'json.loads()\' ,bu

相关标签:
3条回答
  • 2021-02-04 20:28

    In this case
    You need to read the content of file

    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer
    


    
        import json
    
        f = open("string.txt", 'w')
        f1 = open("stringJson.txt", 'r')
        data = json.loads(f1)
        f.write(json.dumps(data, indent=1))
        f.close()
    
    
    0 讨论(0)
  • 2021-02-04 20:36

    It really depends on how your txt file is structured. But suppose you have a structured txt file like the following:

    BASE|30-06-2008|2007|2|projected
    BASE|30-06-2007|2010|1|projected
    BASE|30-06-2007|2009|3|projected
    BASE|30-06-2007|2020|2|projected
    ...
    

    You could use a script like this:

    import codecs
    import json
    
    import numpy as np
    import pandas as pd
    
    raw_filepath = "your_data.txt"
    
    field_names = [
        "Scenario",
        "Date",
        "Year",
        "Quarter",
        "Value"
    ]
    
    data_array = np.genfromtxt(raw_filepath, delimiter="|", dtype=None, encoding="utf-8")
    df = pd.DataFrame.from_records(data_array)
    df.columns = field_names
    result = df.to_json(orient="records")
    parsed = json.loads(result)
    out_json_path = "your_data.json"
    
    ### saves pandas dataframe in .json format
    json.dump(
        parsed, codecs.open(out_json_path, "w", encoding="utf-8"), sort_keys=False, indent=4
    )
    
    

    Explanation

    To load a dataset in Numpy, we can use the genfromtxt() function. We can specify data file name, delimiter (which is optional but often used), and number of rows to skip if we have a header row. The genfromtxt() function has a parameter called dtype for specifying data types of each column(this parameter is optional). Without specifying the types, all types will be casted the same to the more general/precise type and numpy will try infer the type of a column.

    In this part df.to_json(orient="records") we are Encoding/decoding a Dataframe using 'records' formatted JSON. Note that index labels are not preserved with this encoding. This, way, we can have an output like this, as described in the Pandas Documentation:

    >>>result = df.to_json(orient="records")
    >>>parsed = json.loads(result)
    >>>json.dumps(parsed, indent=4)  
    [
        {
            "col 1": "a",
            "col 2": "b"
        },
        {
            "col 1": "c",
            "col 2": "d"
        }
    ]
    
    0 讨论(0)
  • 2021-02-04 20:53
    import json
    with open("string.txt", "rb") as fin:
        content = json.load(fin)
    with open("stringJson.txt", "wb") as fout:
        json.dump(content, fout, indent=1)
    

    See http://docs.python.org/2/library/json.html#basic-usage

    0 讨论(0)
提交回复
热议问题