Page 1 of 2

Load from file

Posted: Thu May 21, 2020 7:24 am
by zxretrosoft
Hello LÖVE friends,
I'm new here and I'd like to ask. I can't figure it out experimentally ^^

I need to make a simple script to load CSV so that I can then read it into the arrays.

A small example of how it works in PureBasic e.g.

Code: Select all

ReadFile (0, "c: \ test.csv", # PB_Ascii)
i = 0
While Not Eof (0)
   a (i) = ReadString (0)
   i = i + 1
Wend
CloseFile (0)
This will take each line from the CSV to a(i) and then I can work on that data further (eg divide it into other fields using MID $).

I apologize for my English, perhaps my question is understandable.
Thank you in advance! :nyu:

Re: Load from file

Posted: Thu May 21, 2020 1:03 pm
by zorg
Hi and welcome to the forums!

Löve has a line iterator function built-in: love.filesystem.lines

That said, everything in love.filesystem (except for DroppedFiles) only has access to two places, the project's save directory and the directory where your main.lua is (or the inside of the .love file, if you already packaged the files up).

Something like this would work:

Code: Select all

local lines = {} -- empty table
for line in love.filesystem.lines("test.csv") do -- needs to be either next to main.lua or in the save folder.
  table.insert(lines, line)
end
Feel free to ask further questions if something's not clear!

Re: Load from file

Posted: Thu May 21, 2020 3:29 pm
by zxretrosoft
Thank you so much! It seems to be working properly! :awesome:

Just a little more question, please. My native language uses hooks, commas, rings ... (for example: kůň, třmen, důrazně...)
When I accidentally load a word from CSV / TXT, it gives an error:
main.lua: 8 UTF-8 decoding error: Ivalid UTF-8

Where can I set this up?

Re: Load from file

Posted: Fri May 22, 2020 1:53 pm
by zxretrosoft
And one more question please :crazy:

And now, I have data in the field, e.g.

lines[1] = 150
lines[2] = 200
lines[3] = 350
...

How to make SAVE to file?

Thank you so much again! :nyu:

Re: Load from file

Posted: Fri May 22, 2020 3:13 pm
by pgimeno
zxretrosoft wrote:
Fri May 22, 2020 1:53 pm
And one more question please :crazy:

And now, I have data in the field, e.g.

lines[1] = 150
lines[2] = 200
lines[3] = 350
...

How to make SAVE to file?

Thank you so much again! :nyu:
You can use this:

Code: Select all

love.filesystem.write(filename, table.concat(lines, "\n"))
As for your previous question, you haven't given enough information for anyone to figure out what you're doing and what part of your code is causing the error.

Re: Load from file

Posted: Fri May 22, 2020 6:58 pm
by zxretrosoft
Thank you so much, guys!

I can't SAVE it yet. Apparently I'm doing something wrong :neko:

I have a text file (CSV), example:
513911; 276871
123456; 158947

The numbers (first line for example) I get into the arrays:
x(1)=513911
x(2)=276871

Code: Select all

local x = {}
local lines = {}

for line in love.filesystem.lines("test.txt") do
  table.insert(lines, line)
end

for i = 1, string.len(lines[1]) do
  if string.sub(lines[1], i, i) == ";" then
    pozice = i
    break
  end
end

x[1] = string.sub(lines[1], 1, pozice-1)
x[2] = string.sub(lines[1], pozice+1, string.len(lines[1]) )

function love.draw()
  love.graphics.print( tostring(x[1]), 10, 20 )
  love.graphics.print( tostring(x[2]), 10, 35 )
end

And now I need to save x(1), x(2) etc.

But, for example, this is obviously wrong:

Code: Select all

for i = 1, 2 do
  love.filesystem.write("test_output.txt", table.concat(x[i], "\n"))
end

Re: Load from file

Posted: Sat May 23, 2020 1:14 am
by pgimeno
What I meant was to write this:

Code: Select all

love.filesystem.write("test_output.txt", table.concat(x, "\n"))
Without any 'for' loop, without [ i ] indexing.

"\n" means newline. If you want them separated in some other way, just use the appropriate separator.

Re: Load from file

Posted: Sat May 23, 2020 4:48 am
by zxretrosoft
Thank you, I understand :nyu:

But I still have "test_output.txt" empty after running the code
It seems that the values do not write to it.

I have this:

Code: Select all

local x = {}
local lines = {}

for line in love.filesystem.lines("test.txt") do
  table.insert(lines, line)
end

for i = 1, string.len(lines[1]) do
  if string.sub(lines[1], i, i) == ";" then
    pozice = i
    break
  end
end

x[1] = string.sub(lines[1], 1, pozice-1)
x[2] = string.sub(lines[1], pozice+1, string.len(lines[1]) )

love.filesystem.write("test_output.txt", table.concat(x, "\n"))

function love.draw()
  love.graphics.print( tostring(x[1]), 10, 20 )
  love.graphics.print( tostring(x[2]), 10, 35 )
end

Re: Load from file

Posted: Sat May 23, 2020 11:12 am
by pgimeno
zxretrosoft wrote:
Sat May 23, 2020 4:48 am
Thank you, I understand :nyu:

But I still have "test_output.txt" empty after running the code
It seems that the values do not write to it.
Your code works for me. Are you sure the input file has the right contents? Are you sure you're looking at the right output file? test_output.txt should be in the save data directory. See the table here depending on your OS: love.filesystem

Re: Load from file

Posted: Sat May 23, 2020 12:58 pm
by zxretrosoft
Yes, true, I was looking in the wrong directory :roll:

Thank you so much for the important help for me!!!