Home > Software engineering >  Python String without 'quotes' within an overall string
Python String without 'quotes' within an overall string

Time:08-07

I want to create the following json request out. But the timestamp cannot have 'quotes' there. Been trying to fix the message string with no success...... Help!

msg_1 = \
    {
      "jsonrpc" : "2.0",
      "id" : 833,
      "method" : "public/get_tradingview_chart_data",
      "params" : {
        "instrument_name" : ""   str(instr_)   "",
        "start_timestamp" : ""   str(1554373800000)   "",
        "end_timestamp" :  ""  str(1654373800000)   "",
        "resolution" : "1440"
      }

Below is what I am trying to achieve

{
  'jsonrpc': '2.0',
  'id': 833,
  'method': 'public/get_tradingview_chart_data',
  'params': {
    'instrument_name': 'ETH-12AUG22',
    'start_timestamp': 1554373800000,
    'end_timestamp': 1654373800000,
    'resolution': '1440'
  }
}

But I keep getting this

{
  'jsonrpc': '2.0',
  'id': 833,
  'method': 'public/get_tradingview_chart_data',
  'params': {
    'instrument_name': 'ETH-12AUG22',
    'start_timestamp': '1554373800000',
    'end_timestamp': '1654373800000',
    'resolution': '1440'
  }
}

CodePudding user response:

Strings in python are surrounded by either single quotation marks, or double quotation marks. If you would like just the integer value, without quotes: Try using int() instead of str().

example:

int(1554373800000)

CodePudding user response:

You can simply typecast your string to int using

int("Number in String Datatype")

CodePudding user response:

You seem to be using str() when you mean not to.

I don't know what instr_ is, but just in case its an object with a __str__() method I'll leave that one in. The others don't need it:

msg_1 = \
    {
      "jsonrpc" : "2.0",
      "id" : 833,
      "method" : "public/get_tradingview_chart_data",
      "params" : {
        "instrument_name" : str(instr_),
        "start_timestamp" : 1554373800000,
        "end_timestamp" :  1654373800000,
        "resolution" : "1440"
      }
  • Related