I am trying to replace the tab with the expected output as "129hello3 78 0".Its showing same output after running the below mentioned lines of code.
I have given tab space between "129" and "3" and rest others are spaces
a="129 3 78 0"
b=a.replace('\t+','hello')
print(b)
There is no tab in a="129 3 78 0", but rather two spaces. For example, copy and paste tab from the text editor to ensure that the right character is in string a.
Here is the example:
a="129 3 78 0"
b=a.replace(' ','hello')
print(b)
a="129 3 78 0"
b=a.replace('\t','hello')
print(b)
The output is
129hello3 78 0
129hello3 78 0
In order to get this result, please open the editor of your choice (I am using Notepad here) paste the code there, and make sure that you have tab in required spaces (circled in red):
then copy the code from there and insert intoonline compiler of your choice ( I used https://www.programiz.com/python-programming/online-compiler/). Click Run
You are using replace instead of regex.
import re
a="129 3 78 0"
b=re.sub('[\t]+','hello', a)
print(b)
Or if you need replace:
a="129 3 78 0"
b=a.replace('\t','hello')
print(b)
Related
I am working on an excel vba macro which opens some files but I ran into a problem, some files have a special character and I cannot copy it to be able to make a replacement, I even tried to find the ASCII code but it throws me the same code as the common space, I can only see it in MS Word.
The tiny circle is the special char:
You can recognize characters in Word:
Sub PrintASCII()
s = Selection.Text
For i = 1 To Len(s)
Debug.Print Asc(Mid(s, i, 1))
Next
End Sub
Usage: select the symbols and run this Sub. See output in VBE Immediate window
Output
95
95
95
95
160
95
95
95
95
95
13
I stumbled into RED language the other day and spend (more or less literally) the last 24h "learning" it. I exhausted my googling skills trying to find solution for a simple problem that just evades my skills and logic, so hopefully somebody here can lead me to path to righteousness.
As the title suggest, I tried to parse a simple string (any string of random text, really), get the individual char(acter)s and then tried to save them into a variable. (Another string/array/any type really)
The best I could do was with code using:
alpha: charset [#"a" - #"z"]
testString: "this is just random rambling to test parsing!"
as prerequisites and something like this when (trying to) parse:
probe parse teststring [copy text to alpha (append text2 to-integer(to-char text)) to end]
Saves the (first letter) ascii code to text2 and running the script several times properly adds the (same first letter) ASCII code several times in a row:
CONSOLE OUTPUT
>> probe parse teststring [copy text to alpha (append text2 to-integer(to-char text)) to end]
true
== true
>> probe text2
"34"
== "34"
>> probe parse teststring [copy text to alpha (append text2 to-integer(to-char text)) to end]
true
== true
>> probe text2
"3434"
== "3434"
Obviously my parsing is not really "looping" the individual characters of the string or not saving them properly while doing it. Maybe my parsing really takes the whole string from TO till END and I try to then convert that to ASCII code or something else is happening here?
ANY help will be greatly appreciated, as I cannot possibly advance with my RED learning before solving this dilemma and understanding how the parsing really works in RED.
I am not sure, if I understand your question and what you want to achieve, but if you are looking for all the ascii representation of the chars you can get that with
asciis: []
parse teststring [some [set a alpha (append asciis to-integer a )| skip]]
== true
>> asciis
== [116 104 105 115 105 115 106 117 115 116 114 97 110 100 111 109 114 97 109 98 108 105 110 103 116 111 116 101 115 116 112 97 114 115 105 110 103]
some is one of the available words responsible for the looping
There are some issues in your trial. It would give some errors before doing any conversion. text2 is probably declared before. to alpha would give an empty string "" as you try to copy up to the first alpha character. Remember to goes up to and not including the target. You can not convert an empty string to a character. If we assume until now no error occurred, then you are still in front of your string and you go straight to the end of your string.
Some documentation about Red parse. see Iteration about looping
You can debug your parsing either with parse-trace or just put a simple (probe text) after the part of your rule you want to investigate:
How do I delete a character in 6502 basic? Like reverse print...
Would there be a way to channel the del key code into a program? I have tried looking at the reference but there is nothing there for a command.
As OldBoyCoder suggests, it can be done - but the character depends on the platform.
10 PRINT "AB";
20 PRINT "C"
Output on Commodore PET or Apple II:
ABC
For Commodore PET add:
15 PRINT CHR$(20);
Output:
AC
On the Apple II you'd need to use 8 instead of 20 because the I/O firmware is different.
You can also use the string manipulation commands, such as LEFT$, RIGHT$ and MID$, here's an untested example from memory (for the Commodore PET and other Commodore 8-bit machines, probably also works on other variants of BASIC):
0 A$="ABC"
1 PRINT LEFT$(A$,2): REM PRINTS AB
2 PRINT RIGHT$(A$,2): REM PRINTS BC
3 PRINT MID$(A$,2,1): REM PRINTS B
Otherwise, you can over-write characters as long as you know where they are located on the screen.
0 D$=CHR$(17): REM CURSOR DOWN
1 U$=CHR$(145): REM CURSOR UP
2 R$=CHR$(29): REM CURSOR RIGHT
3 L$=CHR$(157): REM CURSOR LEFT
4 PRINT "ABC";L$;" "
I'm using CHR$ codes here for clarity. If you open a string with a double-quote, you can press the UP/DOWN and LEFT/RIGHT keys which will show inversed characters. Printing these inversed characters will do the same as moving the cursor around with the cursor keys.
I am trying to create a logical function in excel for the following conditions.
if value > 85 then the output should be 2
else if value < 85 the output should be 1
else
value < 65 the output should be 0
The formula I have created has the following syntax:
=IF(O25>85;"2";IF(O25<85;"1";IF(O25<65;"0")))
But the output for when it's less than 65 fails.
What have I missed here?
Add an AND statement, or swap order and check if less than 65 first
And statement:=IF(O25>85;"2";IF(AND(O25<85;O25>=65);"1";IF(O25<65;"0")))
Change order: =IF(O25>85;"2";IF(O25<65;"0";IF(O25<85;"1")))
I want to this but i don't know what to do, the only functions it seems to be useful is "DEC.TO.HEX".
This is the problem, i have in one cell this text:
1234
And in the next cell i want the hexadecimal value of each character, the expected result would be:
31323334
Each character must be represented by two hexadecimal characters. I don't have an idea how to solve this in excel avoiding make a coded program.
Regards!
Edit: Hexadecimal conversion
Text value Ascii Value (Dec) Hexadecimal Value
1 49 31
2 50 32
3 51 33
4 52 34
Please try:
=DEC2HEX(CODE(MID(A1,1,1)))&DEC2HEX(CODE(MID(A1,2,1)))&DEC2HEX(CODE(MID(A1,3,1)))&DEC2HEX(CODE(MID(A1,4,1)))
In your version you might need the .s in the function (and perhaps ;s rather than ,s).
DEC2HEX may be of assistance. Use, as follows:
=DEC2HEX(A3)
First split 1234 to 1 2 3 4 by using MID(), then use Code() for each character, and then again concentate. Below is the formula, Y21 is the cell in which 1234 is written
=CONCATENATE(CODE(MID(Y21,1,1)),CODE(MID(Y21,2,1)),CODE(MID(Y21,3,1)),CODE(MID(Y21,4,1)))
1234 >> 49505152