Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:48:15+00:00 2026-05-31T04:48:15+00:00

I have a string in the following form: testline = {key1: value1, key2: {value2-subkey1:

  • 0

I have a string in the following form:

testline = "{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}"

I would like to replace the double-double quotes with a single-double quote (“) and strip the initial and final double quote to finish with a dictionary.

So far, I’ve got something like this, which is very much not doing what I want.

import ast
# testline = testline.strip(")
testline = testline.replace('""', '"')
testlinedict = ast.literal_eval(testline)

This so far yields ValueError: malformed string

I want the final result to be:

testlinedict = {"key1": "value1", "key2": {"value2-subkey1": "value2-subvalue2"}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:48:16+00:00Added an answer on May 31, 2026 at 4:48 am

    The problem is that the double quotes are actually interpreted by Python, but not in the way you expected:

    >>> testline = "{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}"
    >>> testline
    '{key1: value1, key2: {value2-subkey1: value2-subvalue2}}'
    

    This is because in Python, like in C, several string literals following each other are interpreted as one large string, so "abc""def" == "abcdef".

    If you define testdata correctly, your solution works:

    >>> testline = '{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}'
    >>> literal_eval(testline.replace('""', '"'))
    {'key2': {'value2-subkey1': 'value2-subvalue2'}, 'key1': 'value1'}
    

    Or, in case the first and last quote are actually part of the string:

    >>> testline = '"{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}"'
    >>> literal_eval(testline[1:-1].replace('""', '"'))
    {'key2': {'value2-subkey1': 'value2-subvalue2'}, 'key1': 'value1'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following string and I would like to remove <bpt *>*</bpt> and
I have the following code: // Form the continuities list string[] continuities = new
I have following string String str = replace :) :) with some other string;
I have a string like following: CREATE GLOBAL TEMPORARY TABLE some_temp_table_name or CREATE GLOBAL
I have a string like the following: String str = 4*5; Now I have
I have the following string in PHP: <p> [#form id=1]</p> What is the pattern
Let's say I have a string given by user in the following form: single,
I have an HTML string that has the following form: <tr valign=top><td colspan=2 style=padding-bottom:5px;text-align:
I have following string: Test, User < test@test.com >, Another, Test < another@test.com >,
Suppose I have following string: String asd = this is test ass this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.