如果我有字符串"this is a \#test"并将其放入Python中,我将返回'this is a \\#test',这是我正在寻找的行为。但是如果我把它放到Ruby中,我就会返回"this is a #test",没有任何迹象表明有反斜杠存在。我如何使Ruby字符串的行为像Python,也就是说,不自动转义我的#符号?
在Python2.7中,我尝试通过编程将颜色字符串更改为枚举(实际上是IronPython):
from System.Drawing import Color
x = "Red"
而不是这样:
if x == "Red"
SetColor(Color.Red)
我想做一些类似的事情:
SetColor(Color[x])
这看起来是Python3.6的正确语法,但是它抛出了一个异常,所以我不知道我是犯了一个错误,还是Python2.7有其他的解决方案。
不确定为什么转义的单引号没有出现在SQL输出中。最初在Jupyter notebook中尝试过,但在下面的PySpark shell中重现了它。 $ pyspark
SPARK_MAJOR_VERSION is set to 2, using Spark2
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Using Python version 3.6.3 (default, Oct 13 2017 12:02:49)
SparkSession avai
我最近开始学习Python,我希望您能够帮助我解决一个一直困扰我的问题。我一直在用在线学习Python。在练习6中,我遇到了一个问题,在使用%r字符串格式化操作时,它产生了两个不同的字符串。当我打印一个字符串时,我得到了带有单引号(' ')的字符串。另一个是双引号(" ")。
以下是代码:
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and tho
在使用bash shell命令时,有时通过管道输入python并编写一个简短的程序,然后再通过管道将其转换为其他命令是非常有用的。我找不到很多关于编写这样的python程序的文档,尽管在编写编译器或者解释器抱怨的最简单的python程序时,看起来"-c“选项是use..but的选项。如下例所示:
$ python -c "
import os
if os.path.isfile("test"):
print "test is a file"
else:
print "test is not a file"
可能重复:
我已经注意到,我可以同时使用单引号和双引号来分隔python中的字符串,无论哪一个不是用于分隔字符串,我都可以在其中自由使用,而无需转义它。
示例:
ex1 = 'this works'
ex2 = "this works too"
ex3 = "it's much easier to write it's this way"
ex4 = 'but this way, it\'s possible to print out "quotes from other people"
我有一个简单的表(名为test)如下所示:
id | integer
name | character varying(100)
intval | integer
当我尝试使用prepare语句来更新python中的名称时,如下所示。(我使用的是python-pgsql )
>>> for i in db.execute("select * from test"): print i
...
(1, 'FOO', None)
>>> query = "UPDATE test
我正在尝试写一个bash脚本循环遍历字母表,并将字母作为参数插入到字符串中。这就是我尝试过的:
for i in {A..Z}
do
screen -d -m bash -c 'python ~/directory/script.py $i ' //$i is supposed to evaluate to the letter
done
我错过了如何在字符串中输入参数。
我正在尝试通过Bash打印#!/,但它没有打印它,而是打印以下内容。
parth@parth-ubuntu64:$ echo "#!\/"
bash: !\/: event not found
编辑:请允许我再做一次更新。你如何使下列工作?我想我应该使用python而不是bash。我尝试这样做的原因是,我想将它输送到一个需要sudo许可的文件中。这是执行sudo => sudo sh -c“echo This > file”的唯一方法。
sh -c 'echo \'#!/bin/bash \' '