1

I have been trying to solve this issue. I have written:

file.write("pyautogui.write(" + "'" + textEntry + "'" + ")")

but in the file that is written to, the following is written:

pyautogui.write('test
')

I want it all to be on one line. Does anyone know the cause for this? I have tried fixing it, but to no avail.

bad_coder
  • 11,289
  • 20
  • 44
  • 72

4 Answers4

0

it seems like your textEntry variable probably has a newline character at the end.

you should try stripping newlines and spaces from the end of the string if that is all you want to do so something like:

file.write("pyautogui.write(" + "'" + textEntry.rstrip() + "'" + ")")

here is some more info on rstrip: https://stackoverflow.com/a/275025/13282454

Yash
  • 161
  • 1
  • 4
0

I think your textEntry is like that

    textEntry = '''
text
'''

so it has a newline. try to remove it and write simply

`textEntry='text'`
stu_dent
  • 47
  • 6
0

What's happening here is that the textEntry variable likely has a \n character at the end of it, a simple way to solve that is by using strip(). Also, it is generally recommended to use f-Strings instead of doing the + each time. A solution is as follows:

file.write(f"pyautogui.write('{textEntry.strip()}')")
Caden Grey
  • 40
  • 1
  • I didn't even think about there being a newline at the end of the textEntry variable. Also, in layman's terms, does .strip() just remove any extra whitespace or escape characters from a string? – Cnut Cnutson Apr 26 '22 at 01:51
  • @CnutCnutson The `.strip()` method removes all whitespace characters that are at the start and end of the string, so `" hello\n "` becomes `"hello"`. – Red May 15 '22 at 23:13
0

As there will always be a trailing newline character (\n) in the textEntry string, all you'll have to do is use a slice that leaves out the last character in the string:

file.write("pyautogui.write(" + "'" + textEntry[:-1] + "'" + ")")

You can also make use of Python formatted strings:

file.write(f"pyautogui.write('{textEntry[:-1]}')")
Red
  • 26,798
  • 7
  • 36
  • 58