简体   繁体   中英

Create separate strings from multiple lines of text in Python

So I used the Google Vision API to detect the text from an image. The image has a question and 3 multiple choice answers. The Google API returns the correct text, but I need the question and answers to be made into separate strings so I can use the question and each individual answer separately. This wouldn't be hard with just 1 image, but I need the program to be able to separate them no matter how many words are in the question (which always ends in with a '?')

Since the question always ends with '?' my idea was to read through the results, and stop when it reaches a '?' and then from 0-'?' and store it as something like questionResult.

Then for the answer they are all on separate lines so there has to be someway to separate them? These also need to be their own strings/variables.

Clearly I don't know very much on this topic, and i'm not sure what format the Google API results are in, so any help is appreciated. Any help on formatting this post is also appreciated.

Here is my current code

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client 
vision_client = vision.Client('"MY_API_KEY.json')

# The name of the image file to annotate 
file_name = os.path.join(
    os.path.dirname(__file__),
    'hqtest.png') # Your image path from current directory

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()
    image = vision_client.image(
        content=content)

# Performs label detection on the image file
texts = image.detect_text()

# Prints results
print (texts[0].description)

Results of running the code

C:\\Users\\Maxwell\\Desktop\\vision>python test.py Which artist is famous for his "Blue Period"? JMW Turner Pablo Picasso Prince Charles

What I need the result to be

questionResult = 'Which artist is famous for his "Blue Period"?'

answerOne = "J. M. W. Turner"
answerTwo = "Pablo Picasso"
answerThree = "Prince Charles"

I'm not familiar with the Google-Vision api output but from what I understand, you're essentially asking how to split/format a string. The following should help you:

text = """Which artist is famous for
his "Blue Period"?
J. M. W. Turner
Pablo Picasso
Prince Charles"""

tsplit = text.split("?")  # split on ?
# Get first element, replace newlines with spaces, add ? at end
questionResult = tsplit[0].replace("\n", ' ') + "?"

# split everything after the ? on newline (removing the first one)
answerOne, answerTwo, answerThree = tsplit[1].strip().split("\n")

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