Tôi có một tệp JSON có tên example.json
, chứa nội dung sau
[{
"product/productId" : "XXX",
"product/title" : "14k Yellow Gold Butterfly Pendant, 16 ",
"product/price" : "unknown",
"review/userId" : "XXX",
"review/profileName" : "Disappointed Sony customer",
"review/helpfulness" : "1/1",
"review/score" : "4.0",
"review/time" : "1178150400",
"review/summary" : "pretty necklace",
"review/text" : "It is a nice made necklace, and the butterfly pendant looks beautiful. I love it.",
"numOfPositive" : "2",
"numOfNegative" : "0"
},
{ "product/productId" : "XXXX",
"product/title" : "14k Yellow Gold Butterfly Pendant, 16 ",
"product/price" : "unknown",
"review/userId" : "TTT",
"review/profileName" : "A. Thorpe Amazon lover ",
"review/helpfulness" : "1/1",
"review/score" : "4.0",
"review/time" : "1175990400",
"review/summary" : "pretty necklace",
"review/text" : "I bought this necklace on a whim; I love butterflies and it looked so dainty anadfasdfasdfd sweet. It was actually a little more weighty than I expected, although it's not a solid piece. The chain is shiny and nicer than I expected.",
"numOfPositive" : "4",
"numOfNegative" : "0"
}]
Ý nghĩa - Tệp chứa phân bổ các tài liệu được phân tách bằng ",". Làm thế nào tôi có thể đọc từng bộ dữ liệu json một cách tách biệt và xử lý nó [gửi nó đến API REST] trong Python? P.S. Tệp gốc có thể có kích thước lớn ít GB
JSON.LOADS []: Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng json.loads [] method.json.loads [] không lấy đường dẫn tệp, mà là nội dung tệp làm chuỗi, sử dụng FileObject. Đọc [] với json.loads [] chúng ta có thể trả về nội dung của tệp.
Làm cách nào để mở tệp JSON ở chế độ đọc Python?
Làm thế nào để mở và đọc các tệp JSON trong Python ?.
Mở tệp JSON bằng hàm Open [] ..
Sử dụng hàm json.load [] và chuyển đối tượng tệp ..
sự vật | DIGN |
mảng | danh sách |
sợi dây | str |
vô giá trị | Không có |
Số [int] | int |
số [thực] | trôi nổi |
thật | ĐÚNG VẬY |
sai | Sai |
json.load []: json.load [] chấp nhận đối tượng tệp, phân tích dữ liệu JSON, điền từ điển Python với dữ liệu và trả lại cho bạn. & nbsp; json.load[] accepts file object, parses the JSON data, populates a Python dictionary with the data and returns it back to you.
Syntax:
json.load[file object]
Ví dụ: Giả sử tệp JSON trông như thế này: Suppose the JSON file looks like this:
Chúng tôi muốn đọc nội dung của tệp này. Dưới đây là việc thực hiện.
Python3
import
json
f
=
open
[
____1010
json.load[file object]2
=
json.load[file object]4
json.load[file object]5
json.load[file object]6
json.load[file object]7
json.load[file object]8
json.load[file object]9
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject0
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject1
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject2
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject3
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
Output:
JSON.LOADS []: Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng json.loads [] method.json.loads [] không lấy đường dẫn tệp, mà là nội dung tệp làm chuỗi, sử dụng FileObject. đọc [] với json.loads [] chúng ta có thể trả về nội dung của tệp. & nbsp;
If you have a JSON string, you can parse it by using the json.loads[] method.json.loads[] does not take the file path, but the file contents as a string, using fileobject.read[] with json.loads[] we can return the content of the file.
Syntax:
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject
Ví dụ: Ví dụ này hiển thị đọc từ cả tệp chuỗi và JSON. Các tập tin hiển thị ở trên được sử dụng. This example shows reading from both string and JSON file. The file shown above is used.
Python3
import
json
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject7
=
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject9
example.json
0____7 example.json
2
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject2
[
example.json
5example.json
6json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject2
example.json
8‘
json.load[file object]2
=
import
9json.load[file object]5
json.load[file object]6
json.load[file object]7
json.load[file object]8
json.load[file object]9
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject0
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject1
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject2
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject3
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
Output: