Python Forum
Change values in string multiline
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change values in string multiline
#1
Hi,
my question is the following.

can we change the value of some information in a multiline string?

I tested the format but it does not work.

self._values = """
          {
            "email": "{VALUE CHANGE}",
            "password": "{Value Change}"
          }
        """
Thank you
Reply
#2
Hello,
strings in Python aren't really meant to be used that way, there are better options.
Formatting surely is one of them. For example, you can make a function that converts/formats data from a dictionary to a string like you have. Or use JSON etc...
But for your case I think string's replace method would be best suitable.
(website with example)
You may want to throw in some substring search + offsetting and slicing to get your result. But again, it is really cumbersome.
Reply
#3
Could you show what your string looks like and what you want to replace. It's not very clear what you want to achieve.
Your example looks like json string or some kind of unsuccessful attempt at using string formatting
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
self._values = """
          {
            "email": "[email protected]",
            "password": "12345"
          }
        """
I would like to change [email protected] by a string contained in a variable

the same for the password
Reply
#5
OK, this is json string

import json
values = """
          {
            "email": "[email protected]",
            "password": "12345"
          }
        """

my_values = json.loads(values)
my_values['email'] = '[email protected]'
my_values['password'] = '54321'

# print it as json
print(my_values)

#dump back as string
values = json.dumps(my_values, indent=2, sort_keys=True)
# print string values
print(values)
print(type(values))
Output:
{'email': '[email protected]', 'password': '54321'} { "email": "[email protected]", "password": "54321" } <class 'str'>

by the way, where the original string comes from? maybe you don't process it 'correctly' in the first place to end up with string.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output values change akbarza 3 487 Oct-18-2023, 12:30 PM
Last Post: deanhystad
  capturing multiline output for number of parameters jss 3 772 Sep-01-2023, 05:42 PM
Last Post: jss
  Trying to compare string values in an if statement israelsattleen 1 519 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  Getting rid of old string values Pedroski55 3 971 Oct-11-2022, 10:56 PM
Last Post: Pedroski55
  mutable values to string items? fozz 15 2,701 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  change string in MS word Mr_Blue 8 3,218 Sep-19-2021, 02:13 PM
Last Post: snippsat
  Presenting multiline data into single line aaronbuhu 1 1,771 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,490 May-24-2021, 08:48 AM
Last Post: bowlofred
  change numerical values to categorical names JoeOpdenaker 3 2,898 Nov-02-2020, 01:32 PM
Last Post: DeaD_EyE
  Function parameters and values as string infobound 1 1,729 Jul-24-2020, 04:28 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020