Every time I print and in the print there is a special character type è ì the print stops and they exit -------
Related
Basically im trying to get the last modified timestamp of a file without external libraries
OS: Windows
Code:
local cache_out = io.popen("dir /T:W %CD%\\data\\actions\\scripts\\cache.lua", "r")
local cache_data = cache_out:read("*a")
It prints:
My question is: How can start reading from 29/06/2021 to get the modified time which in this case is '04:45'? I know it can be done using string.find or string.gmatch but i have no clue how
Lua uses regex with its own classes for matching characters:
. -- all characters
%a -- letters
%c -- control characters
%d -- digits
%l -- lower case letters
%p -- punctuation characters
%s -- space characters
%u -- upper case letters
%w -- alphanumeric characters
%x -- hexadecimal digits
%z -- the character with representation 0.
To get the class of all characters except the group just use uppercase. For example, if you want to match any character except space (like tab and space) you would use %S.
To match multiple parts of a string you can use parenthesis in string.match() like so
local d, t, _ = string.match(cache_data, "(%d%d/%d%d/%d+)%s+(%d%d:%d%d)%s+([ap].m.)")
and just validate the data for anything other than nil.
For more detailed info I recommend reading this https://www.lua.org/pil/20.2.html
I'm trying to string.format for raw output to the uart using NodeMCU.
I'm trying the function
uart.write(0,string.format("loop %03d local: %02d | gmt %02d:%02d:%02d local %02d/%02d/%04d\n",loops,timezonetime,gmthours,gmtmins,gmtsecs,Nmonth,Nday,Nyear))
but the \n is ignored, and text is concatenated.
print(string.format("loop %03d local: %02d | gmt %02d:%02d:%02d local %02d/%02d/%04d",loops,timezonetime,gmthours,gmtmins,gmtsecs,Nmonth,Nday,Nyear))
works as expected, but I can't control the newline always added by print()
How can I use uart.write and string.format to control the output including the placement and use of newline and other control characters?
The issue a result of newline handling in the LuaLoader that was used for accessing the NodeMCU board. When used with PUTTY, the output is as expected.
Here are the results of more detailed testing. It appears that \r does not work in the string parameter passed to uart.write()
-- uart.write Test
print("______first test____________") -- prime the output with a line and newline
uart.write(0,"asdfasdfasdfasdfasdf") -- no newline
print("______should be at end of same line as asdf...______")
uart.write(0,"asdfasdfasdfasdfasdf(newline)\n") -- with newline
print("______should be on line following asdf...____________")
uart.write(0,"asdfasdfasdfasdfasdf(CR)\r") -- with return only
uart.write(0,"OVERWRITE\n") -- overwrite the first part of asdf line, then newline
print("______should be on newline below OVERWRITE line ____________")
Output results:
dofile("uwtest.lua")
______first test____________
asdfasdfasdfasdfasdf______should be at end of same line as asdf...______
asdfasdfasdfasdfasdf(newline)
______should be on line following asdf...____________
asdfasdfasdfasdfasdf(CR)
OVERWRITE
______should be on newline below OVERWRITE line ____________
>
The expected result is the string "asdfasdfasdfasdfasdf(CR)\r" will be followed by a CR but not LF, causing the terminal cursor to move to the left
This appears to be an issue with the terminal emulation in LuaLoader.
When I connect to the NodeMCU with Putty, I get this output:
> dofile("uwtest.lua")
______first test____________
asdfasdfasdfasdfasdf______should be at end of same line as asdf...______
asdfasdfasdfasdfasdf(newline)
______should be on line following asdf...____________
OVERWRITEsdfasdfasdf(CR)
______should be on newline below OVERWRITE line ____________
>
The Putty output is as expected.
In BASIC I know of two instructions to print to the screen, PRINT, and WRITE, both of which automatically print strings with a newline at the end. I want to print a string without a newline. How can I do this? I'm using GW-BASIC.
Using PRINT with a semicolon will not print a new line:
10 REM The trailing semicolon prevents a newline
20 PRINT "Goodbye, World!";
Source: Rosettacode
A python script fail when trying to encode a supposed utf-8 string in iso-8859-1:
>>> 'à'.encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u0300' in position 1: ordinal not in range(256)
How to know wich charset is that character ? When encode it in utf-8:
>>> 'à'.encode('utf-8')
b'a\xcc\x80'
a then \xcc\x80. I can fount \xcc\x80 in http://www.utf8-chartable.de/unicode-utf8-table.pl?start=768&names=-&utf8=string-literal utf8 table.
But it is utf-8 ? If it is utf-8 why 'à'.encode('utf-8') can't encode this string in iso-8859-1 ?
It's a bit unclear where the 'à' character comes from. In fact, it's a combining character sequence and you need to normalize it. Next python script uses unicodedata module to self-explain and solve your questions:
import sys, platform
print (sys.stdout.encoding, platform.python_version())
print ()
import unicodedata
agraveChar='à' # copied from your post
agraveDeco='à' # typed as Alt+0224 (Windows, us keyboard)
# print Unicode names
print ('agraveChar', agraveChar, agraveChar.encode('utf-8'))
for ins in range( 0, len(agraveChar)):
print ( agraveChar[ins], unicodedata.name(agraveChar[ins], '???'))
print ('agraveDeco', agraveDeco, agraveDeco.encode('utf-8'))
for ins in range( 0, len(agraveDeco)):
print ( agraveDeco[ins], unicodedata.name(agraveDeco[ins], '???'))
print ('decomposition(agraveChar)', unicodedata.decomposition(agraveChar))
print ('\nagraveDeco normalized:\n')
print ("NFC to utf-8", unicodedata.normalize("NFC" , agraveDeco).encode('utf-8'))
print ("NFC to latin", unicodedata.normalize("NFC" , agraveDeco).encode('iso-8859-1'))
print ("NFKC to utf-8", unicodedata.normalize("NFKC", agraveDeco).encode('utf-8'))
print ("NFKC to latin", unicodedata.normalize("NFKC", agraveDeco).encode('iso-8859-1'))
Output:
==> D:\test\Python\40422359.py
UTF-8 3.5.1
agraveChar à b'\xc3\xa0'
à LATIN SMALL LETTER A WITH GRAVE
agraveDeco à b'a\xcc\x80'
a LATIN SMALL LETTER A
̀ COMBINING GRAVE ACCENT
decomposition(agraveChar) 0061 0300
agraveDeco normalized:
NFC to utf-8 b'\xc3\xa0'
NFC to latin b'\xe0'
NFKC to utf-8 b'\xc3\xa0'
NFKC to latin b'\xe0'
==>
My output in my latest 85 program has displays curly braces after the variables and im wondering why this is.
ex: - TOTALS: BEGN-BAL:00100000{ PAYMNT:0000300{ PURCHS:0002500{ FIN-CHRGE:001659{
actually
{ -> indicates +ve sign and
} -> indicates -ve sign
example: for +10 -->in program take s9(02) --> output: " 1{ "
example: for -10--> in program take s9(02) output-> " 1} "
SIGN - WILL BE STORE IN LAST DIGIT AS A CHARACTERS.