Page 1 of 1

Read file with special characters

Posted: 06 May 2021, 17:37
by Ecimeric

Code: Select all

		Array := []
		Loop, Read, file.txt
			Array.Push(StrSplit(A_LoopReadLine, ","))
How can I adapt this code to read special characters in file.txt properly?

Re: Read file with special characters

Posted: 06 May 2021, 18:19
by mikeyww

Code: Select all

Array := []
Loop, Read, %A_ScriptDir%\test.txt
 For k, v in StrSplit(A_LoopReadLine, ",")
  Array.Push(v)

Re: Read file with special characters

Posted: 06 May 2021, 19:14
by Ecimeric

Code: Select all

		Array := []
		Loop, Read, file.txt
			for k, v in (StrSplit(A_LoopReadLine, ","))
                Array.Push(v)
		for index, element in Array
			Menu, MySubmenu2, Add, % element.1, Function
        Menu, MySubmenu, Add, MenuItemName, :MySubmenu2
I did not expect the subsequent code to be relevant, but I am getting a submenu does not exist error.

To clarify: I am trying to get characters like ü to render in MySubmenu2.

Re: Read file with special characters

Posted: 06 May 2021, 19:16
by mikeyww
If you provide an example of the file and the desired effect or output, along with a description of what the script should do, the coding will be more straightforward.

Re: Read file with special characters

Posted: 06 May 2021, 19:31
by Ecimeric
The file looks like Söme wörds,1,234,abc. I want to render Söme wörds in MySubmenu2 instead of having ö appear as gibberish.

Re: Read file with special characters  Topic is solved

Posted: 06 May 2021, 19:40
by mikeyww
This worked at my end.

Code: Select all

Loop, Read, %A_ScriptDir%\temp.txt
 For k, v in StrSplit(A_LoopReadLine, ",")
  Menu, MySubmenu2, Add, %v%, Function
Menu, MySubmenu, Add, Test, :MySubmenu2
Menu, MySubmenu, Show
Return
With only a single line in the file, you do not need a loop.

You might have to save your AHK script file, or your text file, as UTF-8 with BOM signature.

Re: Read file with special characters

Posted: 06 May 2021, 20:59
by Ecimeric
Changing the text file from UTF-8 to UTF-8-BOM did the trick.