0 votes

Hello Everyone,
I have a 2d array something like :

[50,100,150]
[2000,1000,2000]

I want to save this array in a text file, for this I'm using the following code:

func save(content, filename : String):
var file = File.new()
if (file.open(str("res://R//", filename, ".txt"), File.WRITE)== OK):
    for i in range(content.size()):
        file.store_line(str(content[i]) + "\r")
    file.close()

The problem is that the bracket [] appear in the text file. How can I remove [] from the text file

To create the 2D array content I'm using this

func createArrayForTXT():
   var content = []
   content.append("a")
   var array_ = ["b"]
   content.append([])
   for  x in range(array1.size()):
       array_.append(array1[x])
   array_.append(0)
   content[1].append(array_join(array_,","))
   var fileName : String
   for  x in range(aRRAY2.size()):
      content.append([])
      array_ = [aRRAY2[aRRAY2.size()  - 1 - x]]
      for  y in range(array1.size()):
          array_.append(y+x+1)
      array_.append(0)
      content[x+2].append(array_join(array_,","))# + "\r")
      fileName = "textFile1"

   save(content, fileName)

the result of this is

a
[b,1,2,3,4,5,0]
[50,1,2,3,4,5,0]
[40,2,3,4,5,6,0]
[30,3,4,5,6,7,0]
[20,4,5,6,7,8,0]
[10,5,6,7,8,9,0]

and what I need is

a
b,1,2,3,4,5,0
50,1,2,3,4,5,0
40,2,3,4,5,6,0
30,3,4,5,6,7,0
20,4,5,6,7,8,0
10,5,6,7,8,9,0

Thanks!!

in Engine by (132 points)
edited by

1 Answer

0 votes
Best answer

I use this

func save(content, filename : String):
var file = File.new()
if (file.open(str("res://Results//", filename, ".txt"), File.WRITE)== OK):

    for i in range(content.size()):
        if i == 0:
            file.store_line(str(content[i]) + "\r")
        else:
            file.store_line(PoolStringArray(content[i]).join(",") + "\r")

    file.close()
by (132 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.