| 
			
				|  | Cheat Engine The Official Site of Cheat Engine
 
 
 |  
 
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| cd& Newbie cheater
 
 ![]() Reputation: 0 
 Joined: 08 Apr 2023
 Posts: 20
 
 
 | 
			
				|  Posted: Mon Jul 15, 2024 1:27 am    Post subject: how to write/read a csv file(specified row and column)? |   |  
				| 
 |  
				| 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 |  |  
		|  |  
		| AylinCE Grandmaster Cheater Supreme
 
  Reputation: 37 
 Joined: 16 Feb 2017
 Posts: 1528
 
 
 | 
			
				|  Posted: Mon Jul 15, 2024 11:05 am    Post subject: |   |  
				| 
 |  
				| 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
 
 | 
 _________________
 
 |  |  
		| Back to top |  |  
		|  |  
		| cd& Newbie cheater
 
 ![]() Reputation: 0 
 Joined: 08 Apr 2023
 Posts: 20
 
 
 | 
			
				|  Posted: Tue Jul 16, 2024 2:09 pm    Post subject: |   |  
				| 
 |  
				| thank you! this is what i want
 |  |  
		| Back to top |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |  |