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

"}

I need / in between city,state/city,state

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]]

Actual output:

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

L"}

Expected output:

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

"}

asaika
  • 11
  • 6

2 Answers2

0

Try this,

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

Output:

>>> {", ".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'}]

Edit 1:(From comments by OP)

>>> 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'})

Output:

>>> 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'})
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
  • {'Raynham, MA Topsham, ME'} each one is a set so desired output will be {'Raynham, MA/Topsham, ME' – asaika Jul 24 '19 at 06:06
  • TypeError: Object of type 'set' is not JSON serializable I get this error – asaika Jul 24 '19 at 06:14
  • @asaika That is different issue which is not related to this question. You can post a new question and notify me so that I can help, if you can't find the answer. – shaik moeed Jul 24 '19 at 06:18
  • Please check [this](https://stackoverflow.com/questions/8230315/how-to-json-serialize-sets) and [this](https://stackoverflow.com/questions/22281059/set-object-is-not-json-serializable?lq=1). Without seeing your code it's hard to solve your issue. – shaik moeed Jul 24 '19 at 06:44
  • Post the complete traceback. Where you are trying with `JSON`? – shaik moeed Jul 24 '19 at 06:52
  • this is data from api so data from API in loaction should have the desired format conatining / in the loaction – asaika Jul 24 '19 at 07:02
  • Have you copy pasted the code? I didn't get any error. Show the traceback as well. – shaik moeed Jul 24 '19 at 07:29
  • it works as expected but for "Manhattan/Beach,CA/Oyster/Bay,NY" ""Santa/Clara,CA/Fleming/Island,FL" expected output = "Manhattan Beach,CA/Oyster Bay,NY","Santa Clara,CA/Fleming Island,FL It is reading the space after first word – asaika Jul 24 '19 at 07:38
  • how can it be fixed – asaika Jul 24 '19 at 07:43
  • I didn't get you. Can you explain more. Do you want to add `/` only for second word in location? – shaik moeed Jul 24 '19 at 08:43
0
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) 

Output

{'Raynham, MA/Topsham, ME', 'Irvine, CA/Bradenton, FL', 'Savannah, GA/Cary, NC'}
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45