how to modify a certain value or letter within a python text

Asked

Viewed 67 times

0

i have the following text

1050 = N X 0
1260 = N X 0
1470 = N X 0
1680 = N X 0
1890 = N X 0
2100 = N X 0
2310 = N X 0
2520 = N X 0
2730 = N X 0
2940 = N X 0
3150 = N X 0
3360 = N X 0
3570 = N X 0

where ta "X" I want to sub-post for a random number from zero to nine

 (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9]))

to look like this

1050 = N 0 0
1260 = N 1 0
1470 = N 4 0
1680 = N 6 0
1890 = N 8 0
2100 = N 3 0
2310 = N 1 0
2520 = N 1 0
2730 = N 6 0
2940 = N 5 0
3150 = N 3 0
3360 = N 2 0
3570 = N 8 0

I don’t know how to apply

  • 2

    Read the file line by line (open), draw a number (random.choice), replace the value of X (str.replace) and write in the file (f.write). Want to try?

  • I’ll try to do it and I’ll glue the result here

  • I n got it I tried import Random open ('/Users/Felipe/Desktop/GH.txt','r') MM = (Random.Choice([1, 2, 3, 4, 5, 6, 7, 8, 9]) GH.replace('X','MM')

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.