我不是程序员。我是一名艺术家,正在为我正在制作的艺术装置学习 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() ```
最佳答案
你得到的是一个 JSON 字符串。您需要解析 JSON。有一个图书馆。
import json
text = '{"word":"hello","synonyms":["hi","how-do-you-do","howdy","hullo"]}'
data = json.loads(text)
print(" ".join(data["synonyms"]))
关于python - 删除所有非字母字符,保留字符串中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998275/