如何在python的同一字符串变量中同时包含单引号和双引号

时间:2019-02-10 19:44:46

标签: python

如何在python的同一字符串中包含单引号和双引号?

如果我仅使用单引号或双引号,我知道如何解决该问题。我正在使用以下代码:

# Defines the matching rules for Guard.
guard :minitest, spring: "bin/rails test", all_on_start: false do
  watch(%r{^test/(.*)/?(.*)_test\.rb$})
  watch('test/test_helper.rb') { 'test' }
  watch('config/routes.rb')    { integration_tests }
  watch(%r{^app/models/(.*?)\.rb$}) do |matches|
    "test/models/#{matches[1]}_test.rb"
  end
  watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
    resource_tests(matches[1])
  end
  watch(%r{^app/views/([^/]*?)/.*\.html\.erb$}) do |matches|
    ["test/controllers/#{matches[1]}_controller_test.rb"] +
    integration_tests(matches[1])
  end
  watch(%r{^app/helpers/(.*?)_helper\.rb$}) do |matches|
    integration_tests(matches[1])
  end
  watch('app/views/layouts/application.html.erb') do
    'test/integration/site_layout_test.rb'
  end
  watch('app/helpers/sessions_helper.rb') do
    integration_tests << 'test/helpers/sessions_helper_test.rb'
  end
  watch('app/controllers/sessions_controller.rb') do
    ['test/controllers/sessions_controller_test.rb',
     'test/integration/users_login_test.rb']
  end
  watch('app/controllers/account_activations_controller.rb') do
    'test/integration/users_signup_test.rb'
  end
end

# Returns the integration tests corresponding to the given resource.
def integration_tests(resource = :all)
  if resource == :all
    Dir["test/integration/*"]
  else
    Dir["test/integration/#{resource}_*.rb"]
  end
end

# Returns the controller tests corresponding to the given resource.
def controller_test(resource)
  "test/controllers/#{resource}_controller_test.rb"
end

# Returns all tests for the given resource.
def resource_tests(resource)
  integration_tests(resource) << controller_test(resource)
end

我得到这个回报:

kubernetes                                   ClusterIP      10.96.0.1       <none>        443/TCP                      6h30m

nginxingress-nginx-ingress-controller        LoadBalancer   10.104.107.60   <pending>     80:31166/TCP,443:30837/TCP   4h42m

nginxingress-nginx-ingress-default-backend   ClusterIP      10.97.123.87    <none>        80/TCP                       4h42m

我需要的是这个

my_variable = """INSERT into 
mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O'neal''', '''"The Film Title"''')""".encode("utf-8").decode('unicode_escape')

print(my_variable)

2 个答案:

答案 0 :(得分:1)

尝试一下:

my_variable = """INSERT into mytable("UpperAndLowerCaseColumn","Second_Column")
VALUES(\'''O'neal\''', \'''"The Film Title"\''')""".encode("utf8").decode('unicode_escape')

print(my_variable)

答案 1 :(得分:0)

尝试一下: my_variable = '''INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O'neal''', "The Film Title")'''.encode("utf-8").decode('unicode_escape')

相关问题