watsonx.ai

watsonx.ai

A one-stop, integrated, end- to-end AI development studio

 View Only
Expand all | Collapse all

Reading several files sequentially in a notebook

  • 1.  Reading several files sequentially in a notebook

    Posted Thu November 07, 2019 09:09 AM
    Hello, 

    I would like some help, please. 
    I understood how to add and read a single file in the project (-> add to project -> data), but I don't know how to do it for multiple files.

    I am trying to export a project from a Jupiter notebook. For this, it is necessary to read several json files from a folder in a sequential matter in order to create a single dataframe. At the moment I do this:

    # LOAD JSON FILES
    dir_path = "/User/Desktop//Dataset/Snapshots"

    # Open Files
    fnames = sorted([fname for fname in os.listdir(dir_path) if fname.startswith('Snapshot')])
    dfs = []
    for fname in fnames :
    filename = os.path.join(dir_path, fname)
    with open(filename, 'r') as myfile:
    data = myfile.read()
    if data[-1] != ']': data = data + ']'

    # Parse File
    obj = json.loads(data)
    # Convert into Pandas DataFrame
    dfs.append(json_normalize(obj, ))

    # Build a Single DataFrame from all individual DataFrames
    data = pd.concat(dfs)
    data.reset_index(inplace=True, drop=True)

    Can you please help me in how to store this folder containing multiple json files in the project and how to do it to read it?

    Many thanks in advance!

    ------------------------------
    Jéssica
    ------------------------------

    #WatsonStudio


  • 2.  RE: Reading several files sequentially in a notebook

    Posted Fri November 08, 2019 08:57 AM
    Edited by System Admin Wed November 01, 2023 04:52 PM
    Hi  Jéssica,
       I think your coding skills are better than me. I have a few ideas about your problem.
    1. upload to the file to your GITHUB account.
    2. create a list of JSON files name in a cell.
    3. loop through the list and read the file from GITHUB.
    If there are better ideas, I want to learn from you.

    Thanks
    ------------------------------
    min kyawzaw
    Yangon
    ------------------------------