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:
- Tôi có một tệp JSON tĩnh [add.json]
- 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.
- 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
Trong phương thức này, chúng tôi thay thế tất cả các văn bản được lưu trữ trong tệp văn bản, cho điều này, chúng tôi sẽ mở tệp trong chế độ đọc và viết và nó sẽ viết lại tất cả các văn bản.
Python3
s
=
input
{
"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____12Các
{
"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
"
}
]
}
}
]
}
2Output:
Enter text to replace the existing contents: Geeks Text successfully replaced
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
Đơ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.
Python3
Enter text to replace the existing contents: Geeks Text successfully replaced0____8
input
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
0Enter text to replace the existing contents: Geeks Text successfully replaced4
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
2Enter text to replace the existing contents: Geeks Text successfully replaced6
=
input
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
0Enter text to be replaced: Geeks Enter text that will replace: Geekforgeeks Text successfully replaced0
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
2Các
Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
Đơ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.
Enter text to replace the existing contents: Geeks Text successfully replaced0____8
input
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
0Enter text to replace the existing contents: Geeks Text successfully replaced4
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
2Enter text to replace the existing contents: Geeks Text successfully replaced6
=
input
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
0Enter text to be replaced: Geeks Enter text that will replace: Geekforgeeks Text successfully replaced0
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
2Enter text that will replace the existing text: geeks File replaced0
=
Enter text that will replace the existing text: geeks File replaced2
Enter text that will replace the existing text: geeks File replaced3
=
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "dns",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "ip"
}
]
}
}
]
}
2Enter text that will replace the existing text: geeks File replaced6
Enter text that will replace the existing text: geeks File replaced7
Enter text that will replace the existing text: geeks File replaced8
Enter text that will replace the existing text: geeks File replaced9
{
"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=
1
{
"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
"
}
]
}
}
]
}
2Output:
Enter text to be replaced: Geeks Enter text that will replace: Geekforgeeks Text successfully replaced
Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
0Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
1 Enter text to replace the existing contents: Geeks
Text successfully replaced
0Enter text that will replace the existing text: geeks
File replaced
8 Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
4: Using the OS module to replace the file with new text
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks6
=
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks8
Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
5Enter text that will replace the existing text: geeks
File replaced
0__ s
2
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0
Enter text that will replace the existing text: geeks File replaced3
s
5=
s
7Phươ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
"
}
]
}
}
]
}
2Output:
Enter text that will replace the existing text: geeks File replaced
Đơ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 replace the existing contents: Geeks Text successfully replaced0____8
input
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
0Enter text to replace the existing contents: Geeks Text successfully replaced4
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
2Python3
Enter text to replace the existing contents: Geeks Text successfully replaced6
=
input
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
0Enter text to be replaced: Geeks Enter text that will replace: Geekforgeeks Text successfully replaced0
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
2Enter text that will replace the existing text: geeks File replaced0
=
Enter text that will replace the existing text: geeks File replaced2
Enter text that will replace the existing text: geeks File replaced3
=
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "dns",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "ip"
}
]
}
}
]
}
2Enter text that will replace the existing text: geeks File replaced6
Enter text that will replace the existing text: geeks File replaced7
Enter text that will replace the existing text: geeks File replaced8
Enter text that will replace the existing text: geeks File replaced9
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1
Enter text to replace the existing contents: Geeks Text successfully replaced0
Enter text that will replace the existing text: geeks File replaced8
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks4
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks5
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks6
=
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks8
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks0
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
55Output:
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks