How can I import a .csv or .txt file?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By marcorexo

Hi

Problem 1:
Clicking and dragging a .csv file into the project file panel causes this error: ’ Error importing CSV translation: ‘statecode’ is not a valid locale’. It also causes Godot to show the file as a red icon in the folders panel.

I have tried several .csv files and get the same problem.

Problem 2:
Changing the file postfix to .txt causes Godot not to recognise the file as it does not appear in Godot’s folders panel. The following script prints “could not open file”:

if file.open(filename, file.READ) != 0:
		print("opened file")
		#get_csv_line ( String delim=”,” ) 
		while not file.eof_reached():
			var line = file.get_line()
			result[str(index)] = line
			index += 1
		file.close()
	else:
		print("Could not open file")
pass

What am I doing wrong?

Thanks!

If you’re using 3.0+ of the engine, you may be able to still use the files even if they don’t show up in the editor. I think the editor only looks for files related to Godot, e.g. tscn, jpg, png, and json. I’ve been able to do this, yet the method to do that currently escapes me.

Ertain | 2019-02-11 23:52

I’m using 3.2.beta4

If your CSV file does not contain translation info, you need to re-import the resource as “CSV” (default = “CSV Translation”) ! The red cross will be replaced by another icon.

siska | 2020-01-04 13:52

:bust_in_silhouette: Reply From: wombatstampede

You did not specify the value for filename. Either you omitted in your post or that is the problem.

I use a csv file in the project. And although there’s a red cross, this file is readable.

You could however put the file in an own directory (i.e. res://data) and add a .gdignore file to that.
…but this would mean that the file will not be shown in the godot file manager and I doubt that it’ll be included if you export the project. So I’d vote against using .gdignore.

Here’s a part of a working code example:

var groupname=""
var file = File.new()
file.open("res://resources/data/llangollen_aqueduct_trees.csv", file.READ)
while !file.eof_reached():
	var csv = file.get_csv_line ()
    	if csv.size()>=7:
    	    	if csv[2] != groupname:
    	    	    	groupname=csv[2]
file.close()

Thanks wombatstampede. Appreciate your time on this.

marcorexo | 2019-02-12 09:44

:bust_in_silhouette: Reply From: rockgg

I have solved this by opening your .csv file and edit the very first column to a language key (en, en-US, en-UK etc.)

here is the official documentation:
Importing translations — Godot Engine (stable) documentation in English.