Python line.replace multiple strings

cplus

I am replacing the '2010' with a random number:

from random import randint
with open("data.json", "rt") as fin:
    with open("dataout.json", "wt") as fout:
        for line in fin:
            fout.write(line.replace('2010', str(randint(1990, 2007))))

how can I replace two items in one code, that is:

fout.write(line.replace('2099', str(randint(1800, 1900))))
fout.write(line.replace('2010', str(randint(1990, 2007))))
kasravnd

Use two replace() method:

from random import randint
with open("data.json", "rt") as fin:
    with open("dataout.json", "wt") as fout:
        for line in fin:
            fout.write(line.replace('2010', str(randint(1990, 2007))).replace('2099', str(randint(1800, 1900))))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

replace multiple strings in python

How to replace multiple strings in python?

Matching and replace multiple strings in python

replace multiple strings in a file in python3

Python Case Insensitive Replace All of multiple strings

Python replace multiple strings while supporting backreferences

How to match & replace multiple strings with regex in Python

Replace multiple strings with python variable in a sql query

Python: Log multiple strings to one line

Python: Replace multiple strings in text file with multiple inputs

Replace multiple strings at once

Check for multiple replace strings

Replace string with multiple strings

replace strings in multiple lines

Replace multiple strings or texts

Notepad++ : How to find multiple instances of a string on the same line and replace the entire line with found strings

Replace multiple words in multiple strings

Replace multiple strings in multiple files

Python - replace multiple chars in list of strings by other ones based on dictionary

Python: text file replace different strings in multiple lines HOW?

How to replace specific letters in multiple times in strings in python,

Python: How to str.replace multiple different strings with the same replacement?

how do you replace multiple strings in a list using loop for python?

Replace multiple strings with multiple other strings

Multiple line search and replace

replace strings in zsh command line

How do i replace each new line with a whitespace and replace 2 strings with a white space in python?

Python pass multiple strings to a single command line argument

Python: Find and Print strings, multiple instances per line