【问题标题】:How to concatenate string with HTML for PUT request in Python?如何在 Python 中将字符串与 HTML 连接以进行 PUT 请求?
【发布时间】:2021-07-08 10:00:37
【问题描述】:

我当前的代码是:

import requests
import base64
import pandas as pd

pat = 'TO BE FILLED BY YOU'  #CONFIDENTIAL
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic '+authorization
}

df = pd.read_csv('sf_metadata.csv')  #METADATA OF 3 TABLES CURRENCY,SALES,SF_INVENTORIES
df.set_index('TABLE_NAME', inplace=True,drop=True)
df_test1 = df.loc['CURRENCY'] 

x1 = df_test1.to_html()  # CONVERTING TO HTML TO PRESERVE THE TABULAR STRUCTURE

#JSON FOR PUT REQUEST
SamplePage1 = {
  "content": x1
}

#API CALLS TO AZURE DEVOPS WIKI 
response = requests.put(
    url="https://dev.azure.com/xxx/DIFTEST/_apis/wiki/wikis/xxx.wiki/pages?path=SamplePag1&api-version=6.0", headers=headers,json=SamplePage1)

我能够得到以下输出:

我有一个变量:

str1 = "View HIST_DUMPS - Dispatch Dump Record\nDatabase name : PROD_WG\nSchema name : MineOPs"

我需要将 str1 与 x1 连接起来,这样我的输出(在执行 PUT 请求之后)将如下所示: 任何人都可以帮助我如何实现这一目标。谢谢。

【问题讨论】:

    标签: python html json pandas python-requests


    【解决方案1】:

    您可以简单地将str1x1 字符串附加在一起,使用+ 运算符进行字符串连接并更新json dict

    我已将str1 更新为在下面的示例中使用 html

    str1 = "<h1>View HIST_DUMPS - Dispatch Dump Record</h1><br><p><b>Database name</b> : PROD_WG<br><b>Schema name:</b> MineOPs</p>"
    
    table_with_title = str1 + x1
    #JSON FOR PUT REQUEST
    SamplePage1 = {
      "content": table_with_title 
    }
    
    #API CALLS TO AZURE DEVOPS WIKI 
    response = requests.put(
        url="https://dev.azure.com/xxx/DIFTEST/_apis/wiki/wikis/xxx.wiki/pages?path=SamplePag1&api-version=6.0", headers=headers,json=SamplePage1)
    

    产生的输出如下所示(表格在片段中不可见):

    【讨论】:

      猜你喜欢
      • 2010-11-03
      • 1970-01-01
      • 2013-07-08
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多