简体   繁体   中英

Arranging the string of a list in a required format in python

Actually i am parsing some html pages by using scrapy, i have used xpath and fetched some address from some html tag and the result is in the following form

result = ["Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad","Mr Sravan"]

And i want to join the string so i had done below

final_result = ','.join(result)

and got the following result

final_result = "Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad,Mr Sravan"

Here as we can observe that the name in the final_result (address) Mr Sravan is at the end, but generally the name should be before the door or house number in any contacts anywhere right?, so irrespective of number of strings in the list after joining it i should get the name at starting, is there any way to do this? can anyone please let me know how to do this?

Edited Code:

Presently i am parsing four urls at a time, so when i have used xpath(or related) and fetched address from different urls and the result(for example) is as below for each url

final_result = ['Addess_2','Newyork', 'Mr T.Jamal(Name)']
               ['Mr T. Jamal(Name)', 'Addres_1','Extra info'] 
               ['Addres_3','Mr T. Jamal(Name)','Extra info','Related scope'] 
               ['Addres_4','Extra info','Mr T. Jamal(Name)','Related info'] 
               ['Addres_5','Extra info','Related info']  # No name
               ['Addres_6','Extra info1','Related info1']  # No name

Here for all urls in parsing address want to write the code which works for all lists above

You could do it like this :

final_result = ','.join(result[-1:] + result[:-1])


>>> 'Mr Sravan,Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad'

EDIT: Following your comment, if you know that the name is located at the rank i , it could be done like this :

result = ["is", "now", "the name", "at", "first", "place", ":)"]
i = 2
' '.join(result[i:i+1] + result[:i] + result[i+1:])
'the name is now at first place :)'

How about something like this:

  final_result = result[-1] + ', ' + ''.join(result[:-1])

yielding:

Mr Sravan, Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad

-- UPDATE --

This should find the "Mr" string anywhere in your original list and make it work the way you want.

rs = ','.join(result).split(',')
idx = [i for i,j in enumerate(rs) if j.strip().startswith('Mr')][0]
final_result = (rs[idx] +',' + ','.join(rs[1:idx] + rs[idx+1:])).strip()
print final_result

This works with the 4 test strings provided in the update post by OP

-- UPDATE 2 OP changes question so that "Mr" may not be in list --

This code will detect if Mr is in the list and move it to the front as above. It will also detect if it's not in the list and avoid a index out of bounds condition. See output below.

Test strings:

result = ['Addres_4','Extra info','Mr T. Jamal(Name)','Related info'] 
result = ['Addres_4','Extra info','T. Jamal(Name)','Related info'] 

code:

rs = ','.join(result).split(',')
idx = [i for i,j in enumerate(rs) if j.strip().startswith('Mr')]

if len(idx) == 1:  # we found "Mr" in the list
    idx = idx[0]
    final_result = (rs[idx] +',' + ','.join(rs[1:idx] + rs[idx+1:])).strip()
else: # We didn't find "Mr" in the list .. adjust output to your needs
    final_result = (','.join(rs).strip())

print final_result

yield respectively:

Mr T. Jamal(Name),Extra info,Related info
Addres_4,Extra info,T. Jamal(Name),Related info
def name_first(alist):
    """Get the part of the list that begins with 'Mr', then the rest."""
    for i, el in enumerate(alist):
        if el.startswith('Mr'):
            yield alist.pop(i)
            break
    for el in alist:
        yield el

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM