0

I have a corpus of texts packaged into JSON. I have already stripped of some outer JSON layers and I have now lists like the following one

data = [
    '“Ми підписали угоду, яка дозволяє громадянам України подорожувати без віз до Монголії. Це означає, що громадяни України можуть в’їжджати до Монголії без віз на 90 днів упродовж 180 днів” - ',
    {
        'type': 'link',
        'attributes': {
            'href': 'https://www.facebook.com/UkraineMFA/posts/2498886686831903',
            'target': '_blank',
            'rel': None
        },
        'content': [
            'повідомляє сторінка МЗС у Facebook'
        ]
    },
    ' із посиланям на посла України у Польщі Андрія Дещицю.'
]

Each list element is either a text string or yet another JSON element encoded as a Python dict and finally I want to reduce it to its content (niftily already named 'content' here).

What is the Pythonese way of achieving this task?

P.S. The final output should be a line of plain text without any embellishments (neither from Python syntax nor from JSON) left, i.e. for the sample snippet

“Ми підписали угоду, яка дозволяє громадянам України подорожувати без віз до Монголії. Це означає, що громадяни України можуть в’їжджати до Монголії без віз на 90 днів упродовж 180 днів” - повідомляє сторінка МЗС у Facebook із посиланям на посла України у Польщі Андрія Дещицю.
3
  • Could you give an example of what you expect the output to be? Commented Jan 24, 2023 at 15:42
  • done that in the P.S Commented Jan 24, 2023 at 15:48
  • Can you write a function that accepts one of the list elements, and checks whether it's a string or a dict? If it's a dict, can you write the code that gives the desired string? If you have this function, then we can get the desired string fragment for each list element, right? Can you write code to iterate this and put all the pieces together? Does this not solve the problem? Where exactly are you stuck? Commented Jan 24, 2023 at 15:51

1 Answer 1

1

A single-line solution:

data = [
    '“Ми підписали угоду, яка дозволяє громадянам України подорожувати без віз до Монголії. Це означає, що громадяни України можуть в’їжджати до Монголії без віз на 90 днів упродовж 180 днів” - ',
    {'type': 'link', 'attributes': {'href': 'https://www.facebook.com/UkraineMFA/posts/2498886686831903', 'target': '_blank', 'rel': None}, 'content': ['повідомляє сторінка МЗС у Facebook']},
    ' із посиланям на посла України у Польщі Андрія Дещицю.'
]

flat = [item if type(item) is str else item["content"][0] for item in data]

final = "".join(flat)
print(final)

yields

'“Ми підписали угоду, яка дозволяє громадянам України подорожувати без віз до Монголії. Це означає, що громадяни України можуть в’їжджати до Монголії без віз на 90 днів упродовж 180 днів” - повідомляє сторінка МЗС у Facebook із посиланям на посла України у Польщі Андрія Дещицю.'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.