python, \t works different when passed as argument (in Eclipse) [duplicate] - string

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: Split string with multiple delimiters
Convert django Charfield “\t” to tab
I have written a python code in Eclipse which takes delimiters as an argument. When I do
print "Hello",delimiter, "All".
This generates --> Hello \t All, whereas if I overwrite the delimiter with delimiter = '\t' within the code, I get the right output Hello All. I wonder what is the difference? I hope this not just the eclipse thing.

The problem is that what is being passed in from the command line is actually a string of length two "\\t" and not a tab character. You can do the following to your delimiter
delimiter.decode("string_escape"))
that should convert the string '\\t' into '\t'. The answer comes from a duplicate questions here

Related

Removing everything after last hyphen in a string in Bash script? [duplicate]

This question already has answers here:
How can I remove all text after a character in bash?
(7 answers)
Closed last month.
Working on a script where I need to take a string, and remove everything after the last occurence of a certain character. In this case a hyphen.
For example, This-is-a-filename-0001.jpg should result in This-is-a-filename
You can cut strings in bash:
line="This-is-a-filename-0001.jpg"
echo "${line%-*}" # prints: This-is-a-filename
The %-*operator removes all beginning with the last hyphen.
You're looking for a sed within your script, something close to what's below.
sed 's!/[^/]*$!/!'
Generally, I would say, please do research before posting a question like yours since it's relatively easy to find the answers

How come the pound sign '£' in Haskell is shown as '\163' instead of outputting '£'? [duplicate]

This question already has an answer here:
Haskell writes '\n' instead of a newline
(1 answer)
Closed 2 years ago.
I'm trying to write a program that writes the pound sign '£' in Haskell, but it outputs '\163' whenever I try to use it. I'm guessing that this is some alphanumeric code, but how do I get it to display what I want it to? I'm writing to the console, when calling a function that returns '£'.
Thank you.
This was solved by using putStrLn, because print and show do not allow for non-ASCII characters to be shown.

Why doesn't multi line print in python work for single and double quoted strings? [duplicate]

This question already has answers here:
Why are three apostrophes needed for print in Python?
(4 answers)
How do I split the definition of a long string over multiple lines?
(30 answers)
Closed 2 years ago.
This program doesn't work
print("test
test")
This runs as expected
print("""test
test""")
Is this simply because Python interpreter allows multi line strings for triple quotes, or there is some difference in the way it interprets the triple quotes?
When You manually type \n it is interpreted as command to execute, you should use triple quotes to enter newlines(\n) manually. Either """#Your string""" or '''#Your string'''
the triple quotes are also used to define multi line string that why ,
print("""test
test""")
the above code works,
You can also use the \ such as,
print("test \
test")
Have a look at this question-Pythonic way to create a long multi-line string

How to calculate the length of a xterm control sequence [duplicate]

This question already has answers here:
How can I remove the ANSI escape sequences from a string in python
(8 answers)
Closed 3 years ago.
I am looking to format text before passing it to stdout/console. To do this properly I need to know how long a given section of text is after being printed to the console, which requires knowing how much characters will not be printed due to them being escape sequences.
On the web I have found multiple documentations for these sequence commands, but there seems to be no quick and easy way to find out where a given escape sequence command ends unless I was to parse them. Is there a trick/solution to getting the length of any escape sequence command without parsing?
How many characters is this \x1b[38;2;20;60;122m string \x1b[0m in console?
You can use ansifilter:
$ printf '\x1b[38;2;20;60;122mabc\x1b[0m' | ./ansifilter --no-trailing-nl | wc -c
3

Unable To Concatenate 2 Linux Variables Into 1 [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Closed 4 years ago.
I'm trying to concatenate two variables in linux tectia SSH into one variable, separated by "_". For some reason only one of the two variables is printed out.
I've tried to concatenate via " " e.g.:
sample1="$var1_$var2"
or
sample1="$var1 _ $var2"
and I've tried to concatenate directly e.g.:
sample1=$var1_$var2
Would appreciate any help given!
cnt_abr1=ab
cnt_abr2=cd
cnt_abr3=ef
env_abr1=a
env_abr2=b
sample1="$env_abr1_$cnt_abr1"
sample2=$env_abr2_$cnt_abr3
echo $sample1
echo $sample2
Output:
_ ab
ef
Since underscores are effectively letters, bash has no way of knowing when your variable name ends and your literal underscore begins. The proper way to reference the variables is with ${...} in this case, which unambiguously delimits the name from the rest of the command line:
sample1="${env_abr1}_${cnt_abr1}"
sample2=${env_abr2}_${cnt_abr3}
In both cases, the second name does not require special treatment. Any other (semantically valid) non-letter character would do as well, as you pointed out in your comment:
sample1="$env_abr1"_"$cnt_abr1"
sample2="$env_abr2"_"$cnt_abr3"

Resources