简体   繁体   中英

How to print just the name in this JSON object in Python 3.8

Preface - I am in very early stages of python knowledge so I may not know exactly what to google. Have been searching for the last day or so with ideas that are close, but not heping to resolve this.

This is the object example:

"shipping_address": "John Doe\n234 NE Asdf St.\nAsdf Town, Oregon, 14423\nUnited States\n\nPhone: 555-555-2733\nPaypal address: asdf@example.com",

How do I just print the "John Doe" section and not the rest of the info? When I try to print up until the " \ " (minus spaces) I can't seem to get this working. Is this because \ is an operator?

Any input would be hugely helpful.

snippet:

response = requests.get(url).json()
print('Number of orders: '+str(len(response['orders'])))
print()
for order in response['orders']:
    name = order['shipping_address']
    print(name)
    print()

I am assuming the order is a dict you are already getting the text which we need to detect under 'shipping_address' and for the address, the words are separated by "\n" character if so it would be really simple just to use split method.

person_name = order['shipping_address'].split("\n")[0]

For all the other cases, where the shipping address doesn't have a specific character differentiator like the end of line ("\n") you can use the spacy NER module to detect the Person name. As mentioned below.

import spacy
shipping_address =  "John Doe\n234 NE Asdf St.\nAsdf Town, Oregon, 14423\nUnited States\n\nPhone: 555-555-2733\nPaypal address: asdf@example.com"
nlp = spacy.load('en_core_web_sm')
doc = nlp(shipping_address)
person_name = [ ent for ent in doc.ents if ent.label_ == "PERSON" ]

print(person_name)

[John Doe]

For spacy installation Please follow the official guide

You need to split the string on the newline character ( \n ). One way to do that is the .split() method, which returns a list of strings split on a given string. For example:

x = 'This-is-an-example'
print(x.split('-'))
// Result: ['This', 'is', 'an', 'example']

What you want to do is split on the newline character, and take the first element of that list as the name. That would be:

response = requests.get(url).json()
print('Number of orders: '+str(len(response['orders'])))
print()
for order in response['orders']:
    shipping_address = order['shipping_address']
    name = shipping_address.split('\n')[0]
    print(name)
    print()

You can try splitting names by new line ('\n'), and accessing the John Doe portion by index.

response = requests.get(url).json()
print('Number of orders: '+str(len(response['orders'])))
print()
for order in response['orders']:
    name = order['shipping_address']
    print(name.split('\n')[0])
    print()

I think you are confusing the '\' character and the '\n' escape sequence .

Both of these represent a single character. '\' represents the actual backslash character you get when you press the \ key in your keyboard.

On the other hand, '\n' is the escape sequence for the new line character, the character you get when you press Enter on your keyboard.

If you want to get the first line of a string, you have to split the string up until the first new line character not the first backslash . You do it like so:

order['shipping_address'].split('\n')[0]

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