How to input without newline? (Python)


How to input without newline? (Python)



I am trying to create a simple file editor in Python via the console (called PyCons). Here is my current program:


def PyCons(f):
file = open(f, "r")
appe = open(f, "a")
print("n=====nPyCons Editor, v1.2nPython 3.6.1n")
global i
i = 0
for line in file:
i += 1
print(" {}| ".format(i) + line, end='')
for k in range(10000000000000):
print("n {}| ".format(i+1), end='')
inp = input("")
if inp == "@PyCons.save()":
print("n=====")
break
else:
i += 1
appe.write("n" + inp)



I used the end method to make sure that the existing code in the file was printed properly in its line format, but the input function does not have an end attribute. So, when I go to enter code in the editor:


end


input


end


PyCons("magic.html")
...

=====
PyCons Editor, v1.2
Python 3.6.1

1| <p>Hello!</p>
2| <h1>Big text!</h1>
3| <h2>Smaller text!</h2>
#Should be no spaces here
4| <p>More stuff!</p>
#No spaces here either
5| @PyCons.save()

=====



...I get those big, nasty spaces between my inputs. Does anybody know a way to suppress the output of this space, similar to the end='' method used for the print function? Any help would be greatly appreciated, thanks!


end=''


print function



EDIT: Project location for reference: https://repl.it/@xMikee/File-Editing





inp = input("").strip('n') should do the trick
– lenik
Jun 30 at 18:12



inp = input("").strip('n')





Possible duplicate of Possible to get user input without inserting a new line?
– Miket25
Jun 30 at 18:14





Its not clear, but I think his issue might be that newlines are being printed in the terminal, not just the output OP writes to disk. If so- don't pass input a string, call it as input() instead of input(""), that might fix your issue.
– Dillon Davis
Jun 30 at 18:14


input()


input("")





@lenik That did not do anything, unfortunately.
– connectyourcharger
Jun 30 at 18:15





@DillonDavis That did not work, either.
– connectyourcharger
Jun 30 at 18:16




1 Answer
1



The newlines from the inputs are just local echos from the console. You see them on your screen but they aren't actually returned by the input function.


input



The real problem is with the newline you explicitly print before every line number. Remove the n so the line that prints line numbers becomes:


n


print(" {}| ".format(i+1), end='')



Furthermore, the file you're loading may not necessarily have a trailing newline in the end, so you need to detect that and print a newline if that's the case. Note what I added after your first for loop:


for


def PyCons(f):
file = open(f, "r")
appe = open(f, "a")
print("n=====nPyCons Editor, v1.2nPython 3.6.1n")
global i
i = 0
for line in file:
i += 1
print(" {}| ".format(i) + line, end='')
if not line.endswith('n'):
print()
for k in range(10000000000000):
print(" {}| ".format(i+1), end='')
inp = input("")
if inp == "@PyCons.save()":
print("n=====")
break
else:
i += 1
appe.write("n" + inp)





That does not work because of my usage of end='' (it makes it so the new input is on the same line as the previous one).
– connectyourcharger
Jun 30 at 18:19


end=''





That actually works. I tried your code with this change myself. Did you try it?
– blhsing
Jun 30 at 18:20





Maybe insert a print("") between for loops, then remove the newline?
– Dillon Davis
Jun 30 at 18:20


print("")





@blhsing I did try it, that's weird...
– connectyourcharger
Jun 30 at 18:23





@connectyourcharger I just edited my answer with the suggestion I just made to account for the lack of a trailing new line in the file you're loading. Give it a try then.
– blhsing
Jun 30 at 18:44






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API