+ 2
How to know the number of blank spaces consumed by a tabulation ?
Hello everyone, Im writing a program which reads a file and enable to locate(Line, Column) some word in the file. My problem is when it reaches a tabulation(\t) the Column value is incremented by 1 as it was a simple blank space. I have also noticed that the blank spaces depend on text editor. So, how can i get the number of blank spaces consumed by a tabulation ?
3 Answers
+ 1
This has to do on how your text editor processes the so-called "control characters".
On Notepad++, you can configure the number of spaces of a tab as follows:
Settings // Preferences // Language // Tab Settings box // [Default] // Tab size
+ 1
Mario Ălvarez Revuelta Thx for your answer. I know this way. Do u know how can i get it programatically, for example, by using some code ?
0
I use python.
Let's say you have read a line from a *.txt, stored in S:
>>S = 'asd asd asd asd'
being the first and last blank spaces a tab. If you ask python to print it, it will output the before mentioned S, although internally its interpreted as:
'asd\tasd asd\tasd\n'
Well, you can make this S string to become a raw string through:
>>rawS = '%r'%S
This avoids all control characters to be interpreted, writing them as a string. Let's print it:
>>print(rawS)
'asd\tasd asd\tasd\n'
At this point it would be easy to read all characters and if '\' is found and followed by 't', replace them by the desired number of spaces.
I guess there are simpler ways to solve this, but this is what I would do if I were you and had to work this out quickly. I think that you can use raw string conversion using other programming languages, if you're not on python.
Hope it helps.