[GDScript] Load csv file and convert to dictionary

Godot 3.3 rc6

The data stored in Excel cannot be directly saved as a json format file. If you want to save it, you need to install a plug-in, which is more troublesome. But it can be directly saved as a csv file.

I use the WPS table as a demonstration, and write the following two lines of data in it:
insert image description here

NameHealthAttackLevelDescription
namehealthyattack powergradedescribe
beggar212Long-term starvation, resulting in skinny and weak attacks
douchebag322

Save it as a csv file, I named itUnitData.csv
insert image description here

Parse csv file

Create a FileManager.gd file and write the following code

extends Node

## Return Unit's csv file data
func get_unit_data() -> Dictionary:
	var data_list = parse_csv_file("res://UnitData.csv", "Name")
	return data_list


## Parse csv file
## @path file path
## @key which value in the data is recorded in the dictionary as the key value
## @return takes the first row of data as the key value and records it in the form of a dictionary
func parse_csv_file(path: String, key: String) -> Dictionary:
	var file = File.new()
	file.open(path, File.READ)

	# return empty dictionary if no data
    # Separated by ; semicolon, you need to open the csv file yourself to see which character is separated
	 var temp = file . get_csv_line ( ";" ) 
	if temp . size ( )  <=  1 : 
		return  { }

	# The first line is used as the key
	var keys = []
	for i in temp:
		keys.push_back(i)

	# Read a line, but do not operate on the data (this is equivalent to skipping the line)
    # If the second line is the data to be read, then there is no need to add this line of code
	file.get_csv_line()

	# Get the data records into the dictionary, the dictionary can easily get each data
	# For example, we use the column of the name of the data as the key value
    # Then we just use the name to get that row of data
	var data_list =  { } 
	var err_count =  0 	# Number of data errors
	temp = file.get_csv_line(";")
	while temp.size() > 1:
		# If the number of data columns is inconsistent with the number of keys, skip it
		if temp . size ( )  != keys . size ( ) : 
			err_count +=  1 
			if err_count >  10 : 	# If the error exceeds 10 times, no longer read
				 break 
			temp = file . get_csv_line ( ";" ) 
			print_debug ( "The data has Error, inconsistent with the first line n: %d, k: %d"  %  [ temp . size ( ) , keys . size ( ) ] )
			continue

		# Save each row of data to a dictionary
		var data = {}
		for idx in range(temp.size()):
			data[keys[idx]] = temp[idx]
		data_list[data[key]] = data

		# read the next line of data
		temp = file.get_csv_line(";")

	file.close()
	return data_list
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

add toautoloadmiddle
insert image description here
insert image description here
insert image description here
insert image description here

test

Create a new scene Test01 and add a script with the
insert image description here
following code:

extends Node2D

func _ready():
	var unit_data = FileManager.get_unit_data()
	
	# print all data
	print(JSON.print(unit_data, "\t"))
	
	# output the data of the
	 ` beggar` character print ( unit_data [ ' beggar ' ] )

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

The output is as follows:
insert image description here

Tags: [GDScript] Load csv file and convert to dictionary

Godot Godot csv gdscript data

Related: [GDScript] Load csv file and convert to dictionary