Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
zxretrosoft
Prole
Posts: 7
Joined: Thu May 14, 2020 12:51 pm
Location: Prague, Czech Republic
Contact:

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)
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.
I am sorry for poor English

zorg
Party member
Posts: 2873
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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!
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

zxretrosoft
Prole
Posts: 7
Joined: Thu May 14, 2020 12:51 pm
Location: Prague, Czech Republic
Contact:

Thank you so much! It seems to be working properly!

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?
I am sorry for poor English

zxretrosoft
Prole
Posts: 7
Joined: Thu May 14, 2020 12:51 pm
Location: Prague, Czech Republic
Contact:

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!
I am sorry for poor English

pgimeno
Party member
Posts: 2133
Joined: Sun Oct 18, 2015 2:58 pm

zxretrosoft wrote:
Fri May 22, 2020 1:53 pm

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!
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.

zxretrosoft
Prole
Posts: 7
Joined: Thu May 14, 2020 12:51 pm
Location: Prague, Czech Republic
Contact:

Thank you so much, guys!

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

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

I am sorry for poor English

pgimeno
Party member
Posts: 2133
Joined: Sun Oct 18, 2015 2:58 pm

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.

zxretrosoft
Prole
Posts: 7
Joined: Thu May 14, 2020 12:51 pm
Location: Prague, Czech Republic
Contact:

Thank you, I understand

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

I am sorry for poor English

pgimeno
Party member
Posts: 2133
Joined: Sun Oct 18, 2015 2:58 pm

zxretrosoft wrote:
Sat May 23, 2020 4:48 am
Thank you, I understand

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

zxretrosoft
Prole
Posts: 7
Joined: Thu May 14, 2020 12:51 pm
Location: Prague, Czech Republic
Contact: