2015-02-09 324 views
0

我試圖從python中的字符串中刪除撇號。從字符串中刪除撇號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', '1', '-Csv', '-Unit', 'm', '-namePath', destination, '-nameStyle', '%e_%R.csv']) 

文件路徑目前出來與撇號纏的路徑。

`subprocess.call(['', 'weatherForecast/.../filename')]` 

,我想要得到的路徑,而不撇號 即

subprocess.call(['', weatherForecast/.../filename)] 

我已經試過source.strip(" ' ", ""),但它並沒有真正做任何事情。

我試過放在print(filepath)return(filepath)因爲這些將刪除撇號,但他們給我 語法錯誤。

filepath = print(source + str(file)) 
      ^

語法錯誤:無效的語法

我目前的想法。有什麼建議麼?

+1

你能添加你得到的確切的錯誤和異常堆棧跟蹤嗎? – 2015-02-09 05:06:04

+3

這些撇號意味着你正在處理一個「字符串」,所以它們不是真的在字符串本身_...究竟是一個問題呢? – Eithos 2015-02-09 05:08:37

+1

你的問題很混亂。 「print」是否顯示「錯誤」格式?或者你想直接將未加引號的值傳遞給'subprocess.call'?因爲後者_將失敗。 – 2015-02-09 05:10:18

回答

0

字符串對象的strip方法僅從字符串的末尾移除匹配值,它在第一次遇到非必需字符時停止搜索匹配項。

要刪除字符,請將它們替換爲空字符串。

s = s.replace("'", "")