简体   繁体   English

python .get 函数添加了一个换行符,我该如何删除它?

[英]python .get function adds a newline, how do i remove it?

I am using the tkinter add on within python and I am ".get"ing input text from a text widget,我在 python 中使用 tkinter 插件,我从文本小部件“.get”输入文本,

When i do so it decides to add a newline.当我这样做时,它决定添加一个换行符。

I don't want this newline because it messes with my CSV savefile and means i cants read my CSV file properly from python.我不想要这个换行符,因为它弄乱了我的 CSV 保存文件,这意味着我无法从 python 正确读取我的 CSV 文件。

how do i get rid of the "\\n"?我如何摆脱“\\n”?

I have tried ".rstrip" and ".splitlines" neither work.我试过“.rstrip”和“.splitlines”都不起作用。

This is what is displayed by the code...这是代码显示的内容...

How do i get rid of the newline "\\n"?我如何摆脱换行符“\\n”?

Pure code:纯代码:

TextIDNumber = Text(App, height = "1", width = "25", bd = "4")
TextIDNumber.place(x = 550, y = 250)
TextIDNumber.config(font =("bold", 20))
TextSurname = Text(App, height = "1", width = "25", bd = "4")
TextSurname.place(x = 550, y = 350)
TextSurname.config(font =("bold", 20))



def CheckInputUI():
    IDNumber = TextIDNumber.get("1.0", END)
    Surname = TextSurname.get("1.0", END)
    print (Surname, IDNumber)
    print ("")
    print (Surname + IDNumber)

The shortest possible code that reproduces your problem, that we can run on our own machines:重现您的问题的最短代码,我们可以在我们自己的机器上运行:

from tkinter import *


App = Tk()

#This maximises the window
w = App.winfo_screenwidth()
h = App.winfo_screenheight()

App.geometry("%dx%d+0+0" % ((w-10), (h-30)))

TextIDNumber = Text(App, height = "1", width = "25", bd = "4")
TextIDNumber.place(x = 550, y = 250)
TextIDNumber.config(font =("bold", 20))
TextSurname = Text(App, height = "1", width = "25", bd = "4")
TextSurname.place(x = 550, y = 350)
TextSurname.config(font =("bold", 20))

def PrintInfo():
    IDNumber = TextIDNumber.get("1.0", END)
    Surname = TextSurname.get("1.0", END)
    print (Surname, IDNumber)
    print ("")
    print (Surname + IDNumber)

DoneButton = Button(App, text = " Done ", font = ("bold", 18), command = PrintInfo).place(x = 400,y = 800)

I use the prints to display my work to the shell only (so i could find the root of the problem, in other words where the newlines where being added), they server no other purpose.我使用打印件仅向 shell 显示我的工作(因此我可以找到问题的根源,换句话说,在哪里添加换行符),它们没有其他用途。

The text widget guarantees that there is always a newline at the end, even if there isn't one in the actual data.文本小部件保证最后总是有一个换行符,即使实际数据中没有换行符。 To get the all of the data from the widget except this special newline, use "end-1c" (end minus one character) when getting the data:要从小部件中获取除此特殊换行符之外的所有数据,请在获取数据时使用"end-1c" (结尾减去一个字符):

IDNumber = TextIDNumber.get("1.0", "end-1c")
Surname = TextSurname.get("1.0", "end-1c")

If there's a chance that blank lines will get added to the data, you can strip off all trailing whitespace with strip :如果有一个机会,空行将被添加到数据,你可以脱掉所有尾随空白strip

IDNumber = TextIDNumber.get("1.0", END).rstrip()
Surname = TextSurname.get("1.0", END).rstrip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM