去除字符串中所有非字母字符,保留空格

3

我不是程序员,我是一名艺术家,正在学习Python以制作艺术装置。到目前为止,我是在Youtube和Stackoverflow上自学。

话虽如此...

我从API中获取文本,然后将其显示在标签中。原始文本看起来像这样:

{"word":"hello","synonyms":["hi","how-do-you-do","howdy","hullo"]} ,单词根据用户输入而变化。

我只想显示括号内的单词,不要包含标点符号。经过了很多Stackoverflow,我发现re.sub(r"\W", "", text, flags=re.I)(天哪,我不知道它的意思,只知道w、W表示字母/非字母)。这给了我:

"wordhellosynonymshihowdoyoudohowdyhullo

叹气。我怎样才能保留空格?我认为我可以自己去掉前面的17个单词。

完整代码(我很尴尬)。我知道其中部分已经损坏了,我正在逐个处理问题。最终项目是一个程序,返回用户输入的单词的同义词,然后询问用户输出是否正确。结果最终会显示在图表中。有时候,输出结果是有意地错误的,我不需要这个被修复。

'''

import requests
from tkinter import *
import tkinter.messagebox
from tkinter import font
import re
import string


root = Tk ()
Graph = Tk ()

def create_grid(event=None):
    w = c.winfo_width() # Get current width of canvas
    h = c.winfo_height() # Get current height of canvas
    c.delete('grid_line') # Will only remove the grid_line

    # Creates all vertical lines at intevals of 100
    for i in range(0, w, 100):
        c.create_line([(i, 0), (i, h)], tag='grid_line')

    # Creates all horizontal lines at intevals of 100
    for i in range(0, h, 100):
        c.create_line([(0, i), (w, i)], tag='grid_line')

c = Canvas(Graph, height=1600, width=1600, bg='white')
c.pack(fill=BOTH, expand=True)

c.bind('<Configure>', create_grid)






#lets you press enter for query instead of clicking w mouse
#the "forget" objects deletes the button and the widget.
# Should figure out either a timeout, restart, or back-button.
def onReturn(*args):
    command = Buttonclick()
    Button.pack_forget(Button_1)
    Entry_1.pack_forget()
    tkinter.messagebox.showinfo("Help!","This installation measures the accuracy of synoymns delivered. In the next window, please select 'yes' if your synonym was correct, and 'no' if it was not.")
    answer = tkinter.messagebox.askquestion("Accurate?", "Synonym Accurate?")

## Immeidately above and below is the messagebox text. Will eventually connect to a grid.

    if answer == "yes":
        print("thanks!")
    if answer == "no":
        print("FUCK. Sorry.")

    ### Add yes/no actions for message box!

#Gets the user entry when button is pressed, either by mouse or enter key.
def Buttonclick():
    Entry_1.get()


       # this is the API that gets the synonyms - the url must be www.URLURLURL.URL.com/words/ENTRY HERE!!/typeof
        #DO NOT FUCKING TOUCH THIS. IT'S MAGIC AND I DO NOT CONTROL IT.
    url = "https://wordsapiv1.p.rapidapi.com/words/"+Entry_1.get()+"/synonyms"

    headers = {
        'x-rapidapi-host': "wordsapiv1.p.rapidapi.com",
        'x-rapidapi-key': "myapikey"
    }

    punct = "!#$%&'()*+,-./:;?@[\]^_`{|}~"

    response = requests.request("GET", url, headers=headers,)


    newstring = response

    text = response.text


    print(response.text)
    label = Label(root, text= re.sub(r"\W", "", text, flags=re.I), font="helv 18", bg="black", fg="white", )
    label.pack()
    testing = Button(root, text="Press Spacebar to Restart", font="Helv 24", bg="red", fg="white",command=Spacebar)
    testing.bind("<space>",Spacebar)
    testing.pack()


def Spacebar():
    root.configure(background='blue')
    root.geometry("1600x1600+50+50")
    Entry_1 = Entry(root, bg="black", fg="White", font="Helv 48")
    Entry_1.bind("<space>", onReturn)
    Entry_1.pack(expand=1)
    Button_1 = Button(root, text="Type Word, Press Enter For Synonym", font="Helv 24", bg="blue", fg="white", command=Buttonclick)
    Button_1.pack(fill=X, expand=1)
    Label.forget()



#Initial button and text entry box, sized correctly.
root.configure(background='blue')
root.geometry("1600x1600+50+50")
Entry_1 = Entry(root, bg="black", fg="White", font="Helv 48")
Entry_1.bind("<Return>", onReturn)
Entry_1.pack(expand=1)
Button_1 = Button(root, text="Type Word, Press Enter For Synonym", font="Helv 24", bg="blue", fg ="white", command=Buttonclick)
Button_1.pack(fill=X, expand=1)

root.mainloop() ```
3个回答

4
你得到了一个JSON字符串。你需要解析JSON。有一个库可以实现此功能。
import json
text = '{"word":"hello","synonyms":["hi","how-do-you-do","howdy","hullo"]}'
data = json.loads(text)
print(" ".join(data["synonyms"]))

3
您可以使用Json序列化来完成此工作。从API获取的响应格式为JSON
import json 

response = <your response>
json_obj = json.loads(response)
strvalue = " ".join(json_obj["synonyms"])
print(strvalue)

JSON示例:https://docs.python.org/3/library/json.html

(提示:JSON是一种常用的数据格式,可用于存储和交换数据)

1
这个可行!非常感谢!非常令人满意和简单。 - Tooluser

1
你得到的对象称为 JSONdict,它非常棒。
如果你像这样将其赋值给一个变量:
a = {"word":"hello","synonyms":["hi","how-do-you-do","howdy","hullo"]}

你可以像这样引用它们:

for item in a['synonyms']:
     print(item)

为了帮助您,SO需要提供一个完整的可运行示例,以便了解您想要实现的内容。


既然他似乎能够在整个内容上使用正则表达式,我怀疑它已经不是JSON了。也许在你的代码中添加一些关于序列化的内容,否则它将不可避免地导致另一个帖子 :) - TomMP
哇,我不习惯论坛上有人这么客气!我猜我已经有一会儿没离开 Reddit 了。你是指我的项目说明还是完整的代码? - Tooluser
@TomMP 我的理论是他调用了一个在线API,该API返回了一个JSON对象,然后他将其转换为字符串。 - vencaslac
@Tooluser r/learnpython非常有用!如果您提供了您的代码并尝试更详细地解释您要做什么,那肯定会有所帮助。 - vencaslac
1
哈哈,你非常友善,而且说得对。我不是程序员。你再次正确地预测到了 TypeError,并且三次正确地提供了 ilmiacs 的解决方案(它起作用了)。感谢你为 SO 提供了一个很好的介绍。 - Tooluser
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接