How to specify a “tab” in python code? [duplicate] - python

This question already has answers here:
How do I write a "tab" in Python?
(7 answers)
Closed 8 years ago.
Suppose I want to write "welcome" TAB "username" in file.
How can I specify this TAB?
f = open(filename, 'w')
f.write("welcome:"+TAB+"username");

Use \t character:
>>> print('\tsomething')
something
In your case that would be
f.write("welcome:\tusername")

Just:
f.write("welcome:\tusername")

Related

How to read in a key in .bashrc containing "$" from Python? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
I am using os.environ['my_key'] to read in a key from my .bashrc. For example, if my_key="123$abc" were in .bashrc then os.environ['my_key'] would return 123bc.
Is there a trick to read in the full key?
Try to escape $.
my_key="123\$abc"

How to convert data in document using split-Python [duplicate]

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 4 years ago.
list.txt document contain data like
a,b,c,d,e,f
I want to insert above data into list or convert it as list. I tried this code. But it's not correct.
document=open("list.txt","r")
Mylist=[document.read().split(",")]
print(Mylist)
document.close()
with open("list.txt", "r") as Document:
print(Document.read().split(","))

"\n" not working in python when reading files [duplicate]

This question already has answers here:
Passing meta-characters to Python as arguments from command line
(4 answers)
Closed 4 years ago.
Let's say I have an example file named 'greetings.txt' with this in it
Hello\nThere
and this code
f = open("greetings.txt", "r")
readit = f.read()
print(readit)
But the output is
Hello\nThere
What do I do to make the output detect the "\n" and put Word "There" to the 2nd line?
Thanks for your answers!
Try this:
print(readit.replace(r'\n','\n'))
(When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. See here)

Split string when ";" appears [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 5 years ago.
I need to split the string everytime ; shows up.
words = "LightOn;LightOff;LightStatus;LightClientHello;"
Output should be something like this:
LightOn
LightOff
LightStatus
LightClientHello
Simply, everytime it finds ; in a string, it has to split it.
Thank you for help
res = words.split(";")
Refer to this link for more information on split.

cut the right side of a '=' delimited string and storing into a list in python [duplicate]

This question already has answers here:
How to get part of string and pass it to other function in python?
(4 answers)
Closed 8 years ago.
A beginner question
My file looks like -->
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
How can I cut the IP part and store it into a list for further processing?
Please help.
answer = []
with open('path/to/file') as infile:
for line in infile:
answer.append(line.partition('=')[0].strip())

Categories