【问题标题】:need to append / inbetween text in python需要在python中的文本之间附加/
【发布时间】:2019-07-24 04:36:22
【问题描述】:
raw_location = {Raynham, MA Topsham, ME
"Savannah, GA Cary, NC"
 "'Bloomfield Hills,MI Arlington Heights,IL'

"}

我需要/在城市、州/城市、州之间

raw_location = clean(player.xpath('./td[2]//span/text()'))
cleaned_location = re.sub(r"\(\d+\)","", raw_location)
x = [{", ".join(["/".join(w.strip().split(" ")) for w in word.split(",")])} for word in [s for s in cleaned_location]]

实际输出:

{Raynham, MA Topsham, ME
"Savannah, GA Cary, NC"
 "'Bloomfield Hills,MI Arlington Heights,IL'

L"}

预期输出:

{Raynham, MA/Topsham, ME
"Savannah, GA/Cary, NC"
 "'Bloomfield Hills,MI/Arlington Heights,IL'

"}

【问题讨论】:

  • 你尝试过什么?
  • raw_location = clean(player.xpath('./td[2]//span/text()')) clean_location = re.sub(r"(\d+)","/" , raw_location)
  • 你能帮忙解决一下吗

标签: python regex split scrapy


【解决方案1】:
raw_location = {"Raynham ok  , MA Topsham, ME",
"Savannah, GA  Cary, NC",
"Irvine, CA Bradenton, FL"}

newset = set() 
for i in raw_location:
    tem = i.split(',')
    x = tem[1].strip().split()
    newi = tem[0].strip() + ', ' + x[0]+'/'+x[1] + ', ' + tem[1].strip()
    newset.add(newi )
print(newset) 

输出

{'Raynham, MA/Topsham, ME', 'Irvine, CA/Bradenton, FL', 'Savannah, GA/Cary, NC'}

【讨论】:

  • y = x[0].split(' ') IndexError: list index out of range
  • @asaika 我是y = x[0].split(' ')
  • 它按预期工作,但对于“Manhattan/Beach,CA/Oyster/Bay,NY”“”Santa/Clara,CA/Fleming/Island,FL”预期输出 = “Manhattan Beach,CA/ Oyster Bay,NY","Santa Clara,CA/Fleming Island,FL 它正在读取第一个单词后的空格
【解决方案2】:

试试这个,

>>> raw_location = {'Raynham, MA Topsham, ME', 'Irvine, CA Bradenton, FL', 'Savannah, GA Cary, NC'}

输出:

>>> {", ".join(["/".join(w.strip().split(" ")) for w in word.split(",")]) for word in [s for s in raw_location]}
{'Irvine, CA/Bradenton, FL', 'Savannah, GA/Cary, NC', 'Raynham, MA/Topsham, ME'}

#For set of list

>>> [{", ".join(["/".join(w.strip().split(" ")) for w in word.split(",")])} for word in [s for s in raw_location]]
[{'Raynham, MA/Topsham, ME'}, {'Irvine, CA/Bradenton, FL'}, {'Savannah, GA/Cary, NC'}]

编辑 1:(来自 OP 的 cmets)

>>> obj = ({'name': 'Alex Finkelstein/Nathan Mao', 'index': '1', 'location': 'Raynham, MA Topsham, ME'}, {'name': 'George Alexander/Ryan Xiao', 'index': '3', 'location': 'Savannah, GA Cary, NC'}, {'name': 'Bryson Cook/Graham Hadesman', 'index': '4', 'location': 'Sewickley, PA Bradenton, FL'})

输出:

>>> for d in obj:
    d['location'] = ",".join(word.strip().replace(' ', '/') for word in d['location'].split(','))

>>> obj
({'name': 'Alex Finkelstein/Nathan Mao', 'index': '1', 'location': 'Raynham,MA/Topsham,ME'}, {'name': 'George Alexander/Ryan Xiao', 'index': '3', 'location': 'Savannah,GA/Cary,NC'}, {'name': 'Bryson Cook/Graham Hadesman', 'index': '4', 'location': 'Sewickley,PA/Bradenton,FL'})

【讨论】:

  • {'Raynham, MA Topsham, ME'} 每个都是一组,因此所需的输出将是 {'Raynham, MA/Topsham, ME'
  • TypeError: 'set' 类型的对象不是 JSON 可序列化的我收到此错误
  • @asaika 这是与此问题无关的不同问题。如果找不到答案,您可以发布新问题并通知我,以便我提供帮助。
  • 请检查thisthis。没有看到你的代码就很难解决你的问题。
  • 发布完整的回溯。你在哪里尝试JSON
猜你喜欢
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多