0 votes

Is it possible to recreate a structured dictionary from a csv file?

Suppose we want a dictionary like:

onready var Library = {

    "Books":{
        #EXAMPLE OF A BOOK STUFF
        "Cooking 101":{
            "RECIPES":10,
            "SOUND":"COOK_BOOK_SND",},     

        "Psychology": "BOOK_STUFF",
        "Fishing tips": "BOOK_STUFF",
        "Dresses of 20 century": "BOOK_STUFF"
    }
}

In the CSV is like:
enter image description here

How we would import the csv in order to recreate that dictionary?

The goal is to manipulate BIG amount of data using CSV, which will avoid duplicating dictionary structures inside godot scripts.

I tried various methods but always ended up scratching my head as to how implement this.

-Godot 3.2.1 stable official

So far my research has found the following topics:

How to use CSV formated Files with Godot
https://www.youtube.com/watch?v=FiavSnFC8HE

How can I import a .csv or .txt file?
https://godotengine.org/qa/40566/how-can-i-import-a-csv-or-txt-file

GMS function load_csv() in Godot
https://godotforums.org/discussion/20075/gms-function-load-csv-in-godot

asked May 14 in Engine by The_Black_Chess_King (265 points)
edited May 14 by The_Black_Chess_King

2 Answers

0 votes
Best answer

Woops, forgot to post the answer, my bad. I Got it to work properly, it's really quite simple, you just need to wrap your head around a system to use the lines.

for LINECSV in DATA_CSV: # Executed "for" each line of CSV
    if !LINECSV == 0 and LINECSV != DATA_CSV.size()-1: 
        # == 0 because they are my categories, not the data I want.
        #size()-1 is because my DATAFromCSV adds a extra ghost line.    

        var ID = DATAFromCSV[LINECSV][0]
        var Title = DATAFromCSV[LINECSV][1]
        var SubTitle = DATAFromCSV[LINECSV][2]

        #ID, TITLE_CATEGORY and TITLE
        LIBRARY[ID] = {Title:SubTitle}
answered May 21 by The_Black_Chess_King (265 points)
+1 vote

Anything is possible :) I haven't dealt with CSV in Godot but it would certainly be possible to write a parser that gives you dictionary objects.

You might want to reconsider CSV for your database, there are some cool tools that can output JSON in a way that is much easier to parse in Godot eg. CastleDB - take a look at this video https://www.youtube.com/watch?v=TdWWiVd1IxQ

answered May 17 by phiz (94 points)

Looks like a great tool, I just looked it up, but I still wanted to use CSV because I already had a data structure to use and I didn't wanted to change it :>, I eventually got it to work, I post it as an answer.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.