在python中删除行中的空格

我一直在使用此脚本从购物网站中提取价格,以获得获得最佳价格的机会,但是idk为什么我无法从尝试脱衣换衣的线中删除空格,但似乎没有任何作用。 谢谢你的回答 我是医务人员,对编程很感兴趣,这就是为什么我不太熟练 这是代码:

import requests
from bs4 import BeautifulSoup
import re
file = open('weird.txt' , "w+" , encoding='utf8')
out = open("output.txt" , "w+" , encoding = 'UTF-8')
url = "https://www.lioncomputer.com/computer/computer-components/motherboard.html?lion_599=26179%2C26175&product_list_dir=asc"
page = requests.get(url)
page = page.text
soup = BeautifulSoup(page , 'html5lib')
file.write(soup.prettify())
soup2 = soup.find('ol' , class_ = 'products list items product-items')
item = soup2.find_all('div' ,class_ ="product details product-item-details")
for i in item :
    name =i.find(class_= "product-item-link")
    name = name.get_text()
    price = i.find(class_="price-box price-final_price")
    price = price.get_text()
# text = i.find('a' , class_ = 'product-item-link')
    out.write(("{0}:{1}".format(name,price)))
with open('outpuT.txt' , "w+") as f :
    for line in f:
        cleaned = line.replace((re.find((' ')[6])), '')
        cleaned = cleaned.strip()
        f.write(cleaned)
评论
  • cut
    cut 回复

    您可以在python中使用replace函数删除空格。

    newUpdatedString = oldString.replace("  ","")
    

    但是,请记住,此命令将删除所有连续的空格。