python字符串'"':字符串内的单双引号 [英] python string ' " ' : single double quote inside string

查看:115
本文介绍了python字符串'"':字符串内的单双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

双引号如下所示.如果将其放在python字符串中,则会得到此''.在python中,我可以将两个双引号放在字符串'"'中,并将其打印为两个双引号.但是,我不能像以前一样在字符串中加上单引号''.我在Eclipse中使用pydev进行此操作,并且给出了错误:"遇到"\ r"(13),在:" 之后.我正在尝试使用命令管道和文件名执行以下操作:

A double quote looks like this ". If I put this inside a python string I get this ' " '. In python, I can put two double quotes in a string ' "" ' and this gets printed as two double quotes. However, I cannot put a single double quote in a string, as before, ' " '. I am doing this in eclipse with pydev and it gives an error: "encountered "\r" (13), after : "". I am trying to do the following with command pipe and file names:

logA = 'thing.txt'
cmdpipe = os.popen('copy "C:\upe\' + logA + '"' + ' "C:\upe\log.txt"') 

推荐答案

您需要转义反斜杠,否则会做奇怪的事情.

You need to escape the backslashes, otherwise it will do odd things.

logA = 'thing.txt'
cmdpipe = os.popen(
    'copy "C:\\upe\\' + logA + '"' + ' "C:\\upe\\log.txt"')

更加Python化的方式是这样的:

A more pythonic way would be this though:

logA = 'thing.txt'
cmdpipe = os.popen('copy "C:\\upe\\{}" "C:\\upe\\log.txt"'.format(logA))

这篇关于python字符串'"':字符串内的单双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆