从字符串python中删除撇号
我试图在python中删除字符串中的撇号 以下是我试图做的:从字符串python中删除撇号,python,string,Python,String,我试图在python中删除字符串中的撇号 以下是我试图做的: source = 'weatherForecast/dataRAW/2004/grib/tmax/' destination= 'weatherForecast/csv/2004/tmax' for file in sftp.listdir(source): filepath = source + str(file) subprocess.call(['degrib', filepath, '-C', '-msg',
source = 'weatherForecast/dataRAW/2004/grib/tmax/'
destination= 'weatherForecast/csv/2004/tmax'
for file in sftp.listdir(source):
filepath = source + str(file)
subprocess.call(['degrib', filepath, '-C', '-msg', '1', '-Csv', '-Unit', 'm', '-namePath', destination, '-nameStyle', '%e_%R.csv'])
filepath当前显示为带撇号的路径。i、 e 我想得到没有撇号的路径 i、 e 我试过
source.strip(“'”,“)
,但它实际上什么都没做
我尝试过放入print(filepath)
或return(filepath)
,因为它们将删除撇号,但它们给了我
语法错误
filepath = print(source + str(file))
^
SyntaxError:无效语法
我现在没有主意了。有什么建议吗?字符串对象的
strip
方法仅从字符串的末尾删除匹配值,当它第一次遇到非必需字符时,它停止搜索匹配项
若要删除字符,请将其替换为空字符串
s = s.replace("'", "")
这个问题的公认答案实际上是错误的,可能会引起很多麻烦<代码>条带方法
删除前导/尾随字符
。所以,当您要从开始和结束移除角色时,可以使用它
如果改用replace
,则会更改字符串中的所有字符。下面是一个简单的例子
my_string = "'Hello rokman's iphone'"
my_string.replace("'", "")
以上代码将返回Hello rokamns iphone。正如你所看到的,你在s。这不是你需要的东西。但是,我相信您只解析位置,而不解析该字符。这就是为什么当时你可以使用它的原因
对于解决方案,您只做了一件错事。当您调用strip
方法时,会在前后留下空间。正确的使用方法应该是这样
my_string=“'Hello world'”
my_string.strip(“'))
但是,这假设您得到了”
,如果您从响应中得到“
,您可以像这样更改引号
my_string=''Hello world''
我的字符串.strip(“”)
您能添加您得到的确切错误和异常堆栈跟踪吗?这些撇号表示您正在处理一个字符串
,因此它们实际上不在字符串本身中。。。这到底是个什么问题?你的问题很让人困惑。打印是否显示“错误”格式?或者您想将未加引号的值传递给子流程。直接调用
?因为后者会失败。程序需要运行一个没有单撇号的目录。当前,subprocess.call(“”,filepath)显示为subprocess.call(“”,'weatherForecast/../file')。我需要它作为subprocess.call(“”,weatherForecast/../file)@user3886109用Python术语来说,weatherForecast/../file
你期望它是什么?您知道字符串周围的引号不包括在实际调用中吗?对于例如subprocess.call(['ls','/'])
将运行ls/
,而不是ls'/'
。。。
s = s.replace("'", "")
my_string = "'Hello rokman's iphone'"
my_string.replace("'", "")