Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


how to write/read a csv file(specified row and column)?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
cd&
Newbie cheater
Reputation: 0

Joined: 08 Apr 2023
Posts: 20

PostPosted: Mon Jul 15, 2024 1:27 am    Post subject: how to write/read a csv file(specified row and column)? Reply with quote

Hello
I want to read from a CSV file and write to.
Is it possible to read from specific rows and columns?
Is it possible to write to?

example
in test.csv

1,3,4,5
2,4,5,6
3,6,9,12

I want to read row3column2. and if the number is 5, I want to rewrite 7.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 34

Joined: 16 Feb 2017
Posts: 1402

PostPosted: Mon Jul 15, 2024 11:05 am    Post subject: Reply with quote

CSV data, read / write:
Code:
-- csv file read
function readCSV(file)
 local lines = {}
 for line in io.lines(file) do
  local row = {}
  for value in string.gmatch(line, "([^,]+)") do
   value = (value):gsub(",","")
   table.insert(row, value)
  end
  table.insert(lines, row)
 end
 return lines
end

-- csv file write
function writeCSV(file, data)
  local file = io.open(file, "w")
  for _, row in ipairs(data) do
    file:write(table.concat(row, ",") .. "\n")
  end
  file:close()
end

-- Example usage:

path = getTempFolder().."\\"
local data = {
 {"Name", "Surname", "Age"},
 {"Alen", "Walker", "30"},
 {"Aylin", "CE", "25"} -- ( Example ;)
}

writeCSV(path.."example.csv", data)

local loadedData = readCSV(path.."example.csv")

for _, row in ipairs(loadedData) do
     if _==3 then -- lines 3
      --print(table.concat(row," "))
      print("Name: "..row[1])
      print("Surname: "..row[2])
      print("Age: "..row[3])
     end
end


Your code:
Code:
-- csv file read
function readCSV(file)
 local lines = {}
 for line in io.lines(file) do
  local row = {}
  for value in string.gmatch(line, "([^,]+)") do
   value = (value):gsub(",","")
   table.insert(row, value)
  end
  table.insert(lines, row)
 end
 return lines
end

-- csv file write
function writeCSV(file, data)
  local file = io.open(file, "w")
  for _, row in ipairs(data) do
    file:write(table.concat(row, ",") .. "\n")
  end
  file:close()
end


-- Example usage:
path = getTempFolder().."\\"

local data = {
 {"Name", "Surname", "Age"},
 {"Alen", "Walker", "30"},
 {"Aylin", "CE", "25"} -- ( Example ;)
}

writeCSV(path.."example.csv", data)

function checkReplaceCSV(tbl,ln,cl,old,new)
local newTbl = {}
local t2 = {}
  for _, row in ipairs(tbl) do
     if _==ln then
       for k,l in ipairs(row) do
         if k==cl then
           if l==old then
             row[k] = new
           end
         end
       end
       t2 = {table.concat(row, ",") .. "\n"}
     else
       t2 = {table.concat(row, ",") .. "\n"}
     end
      --for m,n in pairs(t2) do print(m,n) end
     table.insert(newTbl,t2)
  end
  return newTbl
end

local loadedData = readCSV(path.."example.csv")

-- {"Aylin", "CE", "25"} --> -- {"Aylin", "CE", "22"}
-- > table --> lines --> column --> Value --> newValue = replace ..
local replaceData = checkReplaceCSV(loadedData,3,3,"25","22")

-- update
writeCSV(path.."example.csv", replaceData)

-- check

local loadedData2 = readCSV(path.."example.csv")

  for _, row in ipairs(loadedData2) do
    print(table.concat(row, ","))
  end

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
cd&
Newbie cheater
Reputation: 0

Joined: 08 Apr 2023
Posts: 20

PostPosted: Tue Jul 16, 2024 2:09 pm    Post subject: Reply with quote

thank you!
this is what i want
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites