我無法使用下面的代碼在同一行上打印一個字的序列號和定義。任何意見將不勝感激。我已閱讀了此前詢問的Q & A,但沒有運氣。字符串和變量不會連接在同一行上
from bs4 import BeautifulSoup
import requests
loops = ""
while loops == "":
url = "http://dictionary.reference.com/browse/"
c = 1
word = input("Enter word (0 to quit): ")
if word == "0":
break
data = requests.get(url + word)
soup = BeautifulSoup(data.text, "html.parser")
data1 = soup.find_all("div",{"class":"def-content"})
print("The meaning/s of " + word + " is/are:")
for i in data1:
if i.string != None:
print(c, i.string) # Trying to print serial number and definition on same line
c = c + 1
什麼錯誤是什麼呢?或者你的輸出與你期望的有什麼不同? –
沒有錯誤,我只是希望我的輸出的兩個組件(i)序列號和(ii)單詞含義出現在同一行上,但我得到「1」,然後在下一個單詞的含義第三行是「2」,第四行是第二個意思,依此類推。謝謝。 – ks3000
所以你的'i.string'在開頭包含'\ n'。什麼大不了的。嘗試使用'stripped_string',而不是如果它不適合你的需求... –