How to change a 1 line output to multiple lines - python

I have this line in a .txt file:
I just got home.\nDid you make lunch?\nOh, I see...
It's just one line, it's formatted like this for a game, so it is exactly like this, with the \n's.
What I need is that when I print this, after I store it into a variable or list, to print it on multiple lines, respecting the \n. Like this:
I just got home.\n
Did you make lunch?\n
Oh, I see...
I can't think of a way to modify the output to make a new line after the program displays each \n. How would I do this?

Using of str.replace() function would be enough:
s="I just got home.\nDid you make lunch?\nOh, I see..."
print(s.replace("\n", "\\n\n"))
The output:
I just got home.\n
Did you make lunch?\n
Oh, I see...

Related

How to delete comments that are on the same line as the string i need with regex

so im trying to separate the twitter ids into a new file to avoid clutter on the main code file but i want to be able to actually know what the heck each id is so i need to keep the comments in.
iv tried a few ways to get rid of this with regex but the best iv done is just print them :/
heres the code
for line in open("userids.txt"):
lin=re.sub(r'(?m)^ *#.*\n?', '', line)
li=re.search(r'?!#(\w+)', lin)
print(li)
the re.sub finds the line as a WHOLE but i want to preferably just ignore the comment entirely in one line rather than 2 like im trying here. i have tried so many different ways iv found on the internet and none work. this is the only code that even prints back the line.
format in the .txt file is
Twitterid #Twitterhandle
but i would like it to just be
Twitterid
i managed to get this working but the code is probably longer than it needs to be and the regex is more than likely unneeded in this case.
heres the code i used instead that returns JUST the twitter id for each line and also ignores comment lines.
lin=re.sub(r'(?m)^ *#.*\n?', '', line)
clin=lin.strip()
cleanlin=clin.split("#",1)
id=cleanlin[0]
print(id)

file.read() output differences

I know there are a lot of questions about file.read() but I dont think any of them answer this....
Can someone tell me why, if I read a file with say 12 lines, in the interpreter and print it by just typing in filename.read(), it is presented on one continuous line with the EOL character to signify each line?
Then, if I do the same but create define an object, say file-output = filename.read(), and then print that object, it then prints out as it appears in the file?
Does what I said make sense????
Many thanks

cleaning the format of the printed data in python

I am trying to compare two lists in python and produce two arrays that contain matching rows and non-matching rows, but the program prints the data in an ugly format. How can I clean I go about cleaning it up?
If you want to read the file without the \n character, you might consider doing the following
lines = list1.readlines()
lines2 = list2.readlines()
would read your file without the "\n" characters
Alternatively, for each line, you can do .strip("\n")
The "ugly format" might be because you are using print(match) (which is actually translated by Python to print ( repr(match) ), printing something that is more useful for debugging or as input back to Python - but not 'nice'.
If you want it printed 'nicely', you'd have to decide what format that would be and write the code for it. In the simplest case, you might do:
for i in match:
print(i)
(note your original list contains \n characters, that's what enumerating an open text file does. They will get printed, as well (together with the `\n' added by print() itself). I don't know if you want them removed or not. See the other answer for possible ways of getting rid of them.

using the .split method

My question is after using the .split() method, how does python know where to start the split?
For example if I have just opened a txt file using python and I decide to use the line split method like this;
user = line.split(':')[0]
John: hhwoeioawn: 802:0933:Iama John:/home/John:/bin/sh
As you can see, the (":") occurs more than once.
As already said, you question isn't absolutely clear. Looking at your code, it seems that you only want the first word returned by split, in which case, you may also use something like:
user = line[:line.index(":")]
Best regards.

Python beginner - trying to output using sys.stdout.write with print list

What I am looking to do is output the contents of the print list as above at the end of the sys.stdout.write just after the ScriptRes:OK:
My code
print list[0]['VersionString']
sys.stdout.write("ScriptRes:OK:")
I am sure this is probably easy to do but my googling is drawing blanks!
The question is hard to understand, but it sounds like you want the result of the first print right after OK:.
No idea why you mix print and sys.stdout.write (which, as a beginner, you should probably not do unless you know why).
So one obvious way is print "ScriptRes:OK:", list[0]['VersionString']
I suppose you have a dictionary inside a list ,try this
sys.stdout.write('ScriptRes:OK:%s' %list[0]['VersionString'])

Categories

Resources