我正在撰寫一個 python 代碼來通過 cURL 抓取網站資料。我使用https://curlconverter.com/將 cURL 轉換為 python 代碼。代碼作業得很好,但我想根據我的需要進行自定義,就像在這行代碼中一樣
data = '{"appDate":{"startDate":"2022-01-05T18:30:00.000Z","endDate":"2022-01-06T18:30:00.000Z"},"page_number":1,"page_size":20,"sort":{"key":"AppointmentStartTime","order":-1}}'
在“startDate”之后,我想添加我這樣創建的變數(startdate)
可變代碼
我試圖添加這樣的變數,
data = '{"appDate":{"startDate":' startdate ,"endDate":' enddate '},"page_number":1,"page_size":20,"sort":{"key":"AppointmentStartTime","order":-1}}'但這不起作用。
另外添加 ' str(startdate) ' 也沒有幫助。
請誰能告訴我這應該怎么做。
uj5u.com熱心網友回復:
您可能希望使用json 模塊將 json 資料字串轉換為字典。然后您可以自由地操作和匯出您的資料。
import json
raw_data = '{"appDate":{"startDate":"2022-01-05T18:30:00.000Z","endDate":"2022-01-06T18:30:00.000Z"},"page_number":1,"page_size":20,"sort":{"key":"AppointmentStartTime","order":-1}}'
data = json.loads(raw_data) # load json from string to dict
data['appDate']['startDate'] = 23
data['appDate']['endDate'] = 42
print(json.dumps(data)) # export dict to json string
uj5u.com熱心網友回復:
在您顯示的示例中,可能只有一個小錯誤緊跟在 startdate ,之后,帶有撇號。仔細對比:
您的代碼(有錯誤):
data = '{"appDate":{"startDate":' startdate ,"endDate":' enddate '},"page_number":1,"page_size":20,"sort":{"key":"AppointmentStartTime","order":-1}}'
^
SyntaxError: invalid syntax
固定代碼:
data = '{"appDate":{"startDate":' startdate ',"endDate":' enddate '},"page_number":1,"page_size":20,"sort":{"key":"AppointmentStartTime","order":-1}}'
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404644.html
標籤:
上一篇:用不同的分隔符分割字串的某些部分
