Problem with File I/O

Peer support for Air Manager desktop users

Moderators: russ, Ralph

Message
Author
The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Re: Problem with File I/O

#11 Post by The Artful Dodger »

Do you offhand know the details of what the .csv files need to look like? I tried to read one from my spreadsheet program, using static_data and it didn't work. It did work from notepad though. I know there are a lot of options when you open a .csv file.

Sparky

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Problem with File I/O

#12 Post by jph »

c.s.v
comma, separated, values

You only use a comma as a delimiter / separator. There is no other standardisation for csv

Each will be read as a field / data block up to the comma. What you do with it is entirely up to you as it is purely a separator, nothing more
How the file actually 'looks' is related to how it is written, often a line at a time. How it looks in excel is depending on how you import it but you only need to
set the delimiting character and the data will be entered to cells in the delimited format.
so

hello world,this,is a cat

will fill the 3 cells in excel, with 'hello world' 'this' and 'is a cat'

In fact If you open a csv in excel then simply select text - csv file extension from the standard 'open' menu as opposed to import and you have to do nothing more. Data Import is more powerful with more delimiters and options but not needed for basic usage.

however, AM doesn't use CSV files at all so the point is fairly moot unless you have some other use for csv.?
I plan to use CSV files on pi Pico hardware setup which I can then read from and write to from AM via messageport to circumvent the limitation.

As for security, as has been stated, that is NOT an issue.
Joe. CISSP, MSc.

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Problem with File I/O

#13 Post by Keith Baxter »

Hi,

Try a .json file. Looks something like this.
airports.json
(3.38 KiB) Downloaded 47 times
You can test with this code. Put the .json file in the resource folder.

Code: Select all

static_data_load("airports.json", function(data)
	print(data["FAAB"]["AIRPORT"]["AREA"])
end)
Use this website to check if your .json is valid.

https://jsonlint.com/
Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Re: Problem with File I/O

#14 Post by The Artful Dodger »

Hi, guys:
Thanks for the thoughtful replies. I tried desperately most of the day yesterday to cancel my pleas for help but couldn't because the site was down. (Actually, from someone in the office working on "the Q3 accounts" via Facebook , it was a DNS problem, not a site problem!)

Anyhow, the reason why I wanted to cancel my post was because I had solved the problem! It turns out that my spreadsheet program uses a "tab" character as the default delimiter for a .csv file, not a "comma". However, in the end, I decided to use a "comma" delimited .txt file because they look the same, but the static API function in that mode reads a line at a time, not the whole file. I'll post some code once I get it polished. Actually, the hardest part was reading the individual entries. But I've solved that problem as well. So all is good. :>))

Again, thanks for your suggestions.

Sparky

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Problem with File I/O

#15 Post by jph »

The Artful Dodger wrote: Wed Oct 26, 2022 8:28 am However, in the end, I decided to use a "comma" delimited .txt file because they look the same, but the static API function in that mode reads a line at a time, not the whole file. I'll post some code once I get it polished. Actually, the hardest part was reading the individual entries. But I've solved that problem as well. So all is good. :>))
Sparky
Hi,
I am still not quite sure what you are intending, or wanting to do ?
Are you saying you are still attempting to use a comma delimited text file in AM - ?
Joe
Joe. CISSP, MSc.

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: Problem with File I/O

#16 Post by SimPassion »

The Artful Dodger wrote: Wed Oct 26, 2022 8:28 am Hi, guys:
Thanks for the thoughtful replies. I tried desperately most of the day yesterday to cancel my pleas for help but couldn't because the site was down. (Actually, from someone in the office working on "the Q3 accounts" via Facebook , it was a DNS problem, not a site problem!)

Anyhow, the reason why I wanted to cancel my post was because I had solved the problem! It turns out that my spreadsheet program uses a "tab" character as the default delimiter for a .csv file, not a "comma". However, in the end, I decided to use a "comma" delimited .txt file because they look the same, but the static API function in that mode reads a line at a time, not the whole file. I'll post some code once I get it polished. Actually, the hardest part was reading the individual entries. But I've solved that problem as well. So all is good. :>))

Again, thanks for your suggestions.

Sparky
Here's how I'm performing CSV read

Code: Select all

up_debug_bln = user_prop_add_boolean("Enable Debugging", false, "Enable/Disable Debugging Mode")
local local_debug = user_prop_get(up_debug_bln)

local acf_ident			= {}
local acf_idx			= 0
local cols			= 26

for idx = 1,cols,1
do
	acf_ident[idx]={}
end

function data_loading(data)
	if data == nil then
		if local_debug then
			print("===================================================")
			print("					Aircrafts Data loading issue")
			print("===================================================")
		end
		return
	end
		if local_debug then
			print("===================================================")
			print("					Aircrafts Data loaded")
			print("===================================================")
		end
	for key,value in pairs(data) do
		acf_idx = acf_idx + 1
		acf_ident[1][acf_idx] = value["nr"]
		acf_ident[2][acf_idx] = value["acfdesc"]
		acf_ident[3][acf_idx] = value["paneltype"]
		acf_ident[4][acf_idx] = value["cablightcolor"]
		acf_ident[5][acf_idx] = value["primer"]
		acf_ident[6][acf_idx] = value["carbheataltair"]
		acf_ident[7][acf_idx] = value["hsi"]
		acf_ident[8][acf_idx] = value["spd_u"]
		acf_ident[9][acf_idx] = value["low_s"]
		acf_ident[10][acf_idx] = value["hi_s"]
		acf_ident[11][acf_idx] = value["grd_s"]
		acf_ident[12][acf_idx] = value["lbl_s"]
		acf_ident[13][acf_idx] = value["wht_s"]
		acf_ident[14][acf_idx] = value["wht_e"]
		acf_ident[15][acf_idx] = value["grn_s"]
		acf_ident[16][acf_idx] = value["ylw_s"]
		acf_ident[17][acf_idx] = value["vne"]
		acf_ident[18][acf_idx] = value["rrv"]
		acf_ident[19][acf_idx] = value["rrs"]
		acf_ident[20][acf_idx] = value["brv"]
		acf_ident[21][acf_idx] = value["brs"]
		acf_ident[22][acf_idx] = value["author"]
		acf_ident[23][acf_idx] = value["manufacturer"]
		acf_ident[24][acf_idx] = value["model"]
		acf_ident[25][acf_idx] = value["hp"]
		acf_ident[26][acf_idx] = value["acfname"]

	end

end


resource_filename = "aircrafts_data.csv"
resource_meta = resource_info(resource_filename)

if resource_meta ~= nil then
	acf_refs = static_data_load(resource_filename)
	data_loading(acf_refs)
else
	if local_debug then
		print("===================================================")
		print("			Aircrafts Data resource file not found")
		print("===================================================")
	end
end
 
Last edited by SimPassion on Sat Oct 29, 2022 3:54 pm, edited 2 times in total.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Problem with File I/O

#17 Post by jph »

Hi Gilles,
Absolutely lovely code as always. It is always a pleasure to see the master at work.
Can I just ask if there should be a "local cols = 26" in there or are you picking it up from elsewhere in your code?

Seriously, your code (and your fellow countryman - it must be in the water / wine ;) ) is some of, if not THE very very best in the business outside of SI proper. Always top class, extremely well thought out, logical and properly presented.
Joe
Last edited by jph on Sat Oct 29, 2022 3:54 pm, edited 1 time in total.
Joe. CISSP, MSc.

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: Problem with File I/O

#18 Post by SimPassion »

jph wrote: Sat Oct 29, 2022 3:48 pm Hi Gilles,
Absolutely lovely code as always. It is always a pleasure to see the master at work.
Can I just ask if there should be a "local cols = 26" in there or are you picking it up from elsewhere in your code?

Seriously, your code is some of, if not THE very best in the business outside of SI proper. Always top class, extremely well thought out, logical and properly presented.
Joe
Thanks for your kind comments Joe, this doesn't prevent me often doing mistakes in coding (or in comments)
and yes I've missed to add this line (now corrected in the previous post between cod tags) :

Code: Select all

local cols			= 26

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Problem with File I/O

#19 Post by jph »

Coding is a wonderful thing Gilles.
There are many types of coders. Some of the types could be described as

'clinical' (scrubbed clean, boring, but does the job).

'Spiderman' (looks like a pissed up spider has falling in an ink well and then splattered seemingly random words and letters on the page and then had coffee / beer spilt on it :lol: but, surprisingly does actually do something).

'Easy listening' (should be easy reading more likely. Easy to follow, well commented and logical).

Then there is the mark of excellence which I will call

'Inspirational' (easy listening with a really interesting sub plot that generates ideas in others)

You are definitely 'inspirational' along with (I believe) a fellow countryman of yours.
:mrgreen: :D

Joe
Joe. CISSP, MSc.

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: Problem with File I/O

#20 Post by SimPassion »

:D
Joe, reading the last personality you described, I recognize perfectly Jacques with his incredible coding level and ability to share practices on all area

Post Reply