Hướng dẫn replacement words python - từ thay thế python

Tôi chưa quen với Python và những gì tôi đang cố gắng làm là thay thế văn bản/chuỗi trong tệp JSON, từ lệnh shell os python. Tôi có phần nhận được kết quả mà tôi đang tìm kiếm, nhưng nó đang nối thêm khoảng trắng/tạo một dòng mới trong tệp JSON. Đây về cơ bản là những gì tôi đang cố gắng thực hiện:

Nội dung chính

  • Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệp: Removing all text and write new text in the same file
  • Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp: Using Replace function in for loop
  • Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1 Enter text to replace the existing contents: Geeks Text successfully replaced0Enter text that will replace the existing text: geeks File replaced8 Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks4: Using the OS module to replace the file with new text
  • Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5Enter text that will replace the existing text: geeks File replaced0__ s 2
  • Đơn giản cho vòng lặp là một cách thông thường để đi qua mọi dòng trong tệp văn bản đã cho và tìm dòng chúng tôi muốn thay thế. Sau đó, dòng mong muốn có thể được thay thế bằng cách sử dụng hàm thay thế []. & nbsp; Cuối cùng, tệp được mở ở chế độ ghi và nội dung được thay thế được viết trong tệp đã cho. Using fileinput.input[]
  • Làm thế nào để bạn thay đổi một giá trị trong một python json?
  • Làm thế nào để bạn thay thế một từ trong văn bản trong Python?
  • Làm thế nào để bạn tìm và thay thế một phần của một giá trị trong tệp JSON?
  • Làm thế nào để bạn cập nhật chuỗi JSON trong Python?

  1. Tôi có một tệp JSON tĩnh [add.json]
  2. Tôi đang chạy hai lệnh os shell trong Python và lưu trữ đầu ra đó vào các tệp văn bản riêng biệt.
  3. Sau đó tôi muốn lấy các giá trị trong hai tệp TXT đó và thay thế hai chuỗi trong tệp JSON.

Dưới đây là những gì tôi hiện có [để làm cho nó đơn giản, tôi đã thay thế các lệnh AWS CLI thực sự bằng các lệnh đơn giản]

import os
import fileinput

cmd = 'hostname > host.txt'
cmd2 = 'echo mama > echo.txt'

os.system[cmd]
os.system[cmd2]

file = open['host.txt']
contents = file.read[]
with open["out.json", "wt"] as fout:
with open["add.json", "rt"] as fin:
    for line in fin:
        fout.write[line.replace['dns',contents]]

file2 = open['echo.txt']
contents2 = file2.read[]
with open["out2.json", "wt"] as fout2:
    with open["out.json", "rt"] as fin2:
    for line in fin2:
        fout2.write[line.replace['ip', contents2]]

Và đây là kết quả mà nó mang lại:

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}

Như bạn có thể thấy, sau tên và giá trị, nó thực sự thay thế các giá trị, nhưng thêm một dòng mới và tạo ra JSON không hợp lệ.

Đây là tệp tôi thay thế các giá trị trong:

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}

Cảm ơn bạn trước cho bất kỳ câu trả lời. Tôi biết những gì tôi có ở trên là rất bẩn, và tôi chắc chắn rằng phải có một cách tốt hơn/sạch hơn để hoàn thành những gì tôi đang cố gắng làm, nhưng cuối cùng tôi biết tất cả chúng ta phải bắt đầu ở đâu đó và tôi thậm chí không thể bắt đầu Để giải thích tôi biết ơn như thế nào với cộng đồng này vì tất cả sự giúp đỡ mà nó được cung cấp cho đến nay.

Trong bài viết này, chúng tôi sẽ thay thế văn bản trong một tệp bằng Python. Thay thế văn bản có thể xóa toàn bộ nội dung của tệp và thay thế nó bằng văn bản mới hoặc nó có thể có nghĩa là chỉ sửa đổi các từ hoặc câu cụ thể trong văn bản hiện có.

Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệp: Removing all text and write new text in the same file

Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp: Using Replace function in for loop

Python3

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1 Enter text to replace the existing contents: Geeks Text successfully replaced0Enter text that will replace the existing text: geeks File replaced8 Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks4: Using the OS module to replace the file with new text

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
0
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
1____12

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5Enter text that will replace the existing text: geeks File replaced0__ s 2

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
1
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
2
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
4
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
5
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
0
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
8
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2

Output:   

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
4

Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp: Using Replace function in for loop

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1 Enter text to replace the existing contents: Geeks Text successfully replaced0Enter text that will replace the existing text: geeks File replaced8 Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks4: Using the OS module to replace the file with new text

Python3

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5Enter text that will replace the existing text: geeks File replaced0__ s 2

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5Enter text that will replace the existing text: geeks File replaced0__ s 2

Đơn giản cho vòng lặp là một cách thông thường để đi qua mọi dòng trong tệp văn bản đã cho và tìm dòng chúng tôi muốn thay thế. Sau đó, dòng mong muốn có thể được thay thế bằng cách sử dụng hàm thay thế []. & nbsp; Cuối cùng, tệp được mở ở chế độ ghi và nội dung được thay thế được viết trong tệp đã cho. Using fileinput.input[]

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1 Enter text to replace the existing contents: Geeks Text successfully replaced0Enter text that will replace the existing text: geeks File replaced8 Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks4: Using the OS module to replace the file with new text

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5Enter text that will replace the existing text: geeks File replaced0__ s 2

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
61

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
5
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
0
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
8
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2

Output:

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
1

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1 Enter text to replace the existing contents: Geeks Text successfully replaced0Enter text that will replace the existing text: geeks File replaced8 Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks4: Using the OS module to replace the file with new text

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
85
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
86
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
88

Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5Enter text that will replace the existing text: geeks File replaced0__ s 2

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
80
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
13
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
55
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
57

Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới

Các

Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
11
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
12
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
7
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
15
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
07
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
8
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
7
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
20
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
0
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
23
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2

Output:

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
1

Đơn giản cho vòng lặp là một cách thông thường để đi qua mọi dòng trong tệp văn bản đã cho và tìm dòng chúng tôi muốn thay thế. Sau đó, dòng mong muốn có thể được thay thế bằng cách sử dụng hàm thay thế []. & nbsp; Cuối cùng, tệp được mở ở chế độ ghi và nội dung được thay thế được viết trong tệp đã cho. Using fileinput.input[]

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
40____8
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
7
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
0
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
44
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2

Python3

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
46
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
7
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
0
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
10
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
2
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
10
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
12
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
13
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
2
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
16
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
17
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
18
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
19
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
8 0
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
81
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
40
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
18
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
84
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
85
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
86
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "dns",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "ip"
      }
    ]
   }
  }
 ]
}
6
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
88
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
80
{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
55

Output:

{
"Comment": "A new record set for the zone.",
"Changes": [
 {
  "Action": "CREATE",
  "ResourceRecordSet": {
    "Name": "WildburritoPC
 ",
    "Type": "A",
    "TTL": 60,
    "ResourceRecords": [
      {
        "Value": "mama 
"
      }
     ]
   }
  }
 ]
}
8Làm thế nào để bạn thay đổi một giá trị trong một python json?

Làm thế nào để bạn thay đổi một giá trị trong một python json?

Giá trị thay đổi của Python Python trong tệp JSON Câu trả lời...

Nhập JSON ..

với Open ['data.json', 'r+'] là f:.

Dữ liệu = json. Tải [F].

Dữ liệu ['id'] = 134 #

f. Seek [0] #

json. kết xuất [dữ liệu, f, thụt lề = 4].

f. cắt ngắn [] # loại bỏ phần còn lại ..

Làm thế nào để bạn thay thế một từ trong văn bản trong Python?

thay thế [] phương thức trả về một bản sao của một chuỗi ....

old_text là tham số bắt buộc đầu tiên.thay thế [] chấp nhận.Đó là nhân vật hoặc văn bản cũ mà bạn muốn thay thế.....

new_text là tham số cần thiết thứ hai.thay thế [] chấp nhận.....

Đếm là tham số thứ ba tùy chọn đó.thay thế [] chấp nhận ..

Làm thế nào để bạn tìm và thay thế một phần của một giá trị trong tệp JSON?

Đối với mọi dòng, bạn có thể sử dụng Regex để tìm và thay thế.Sau đó, bạn có thể ghi đè tệp hoặc ghi vào một tệp mới.Ngoài ra, bạn có thể tải python json vào và chuyển đổi nó thành một chuỗi.Sau đó thay thế văn bản bằng regex.use regex to find and replace. Then you can either overwrite the file or write onto a new file. Alternatively, you can load the json python in and convert it into a string. Then replace the text using the regex.use regex to find and replace. Then you can either overwrite the file or write onto a new file. Alternatively, you can load the json python in and convert it into a string. Then replace the text using the regex.

Làm thế nào để bạn cập nhật chuỗi JSON trong Python?

Việc cập nhật một đối tượng JSON trong Python cũng đơn giản như sử dụng chức năng Cập nhật [] tích hợp từ gói JSON mà chúng tôi đã nhập.Phương thức cập nhật được sử dụng để thêm một cặp giá trị khóa mới vào chuỗi JSON mà chúng tôi đã khai báo trong mã của chúng tôi.using the built-in update[] function from the json package we have imported. The update method is used to add a new key-value pair to the JSON string that we declared in our code.using the built-in update[] function from the json package we have imported. The update method is used to add a new key-value pair to the JSON string that we declared in our code.

Bài Viết Liên Quan

Chủ Đề