導航:首頁 > 編程語言 > python變數替換

python變數替換

發布時間:2022-05-19 09:59:40

Ⅰ 請問python中這個為什麼能夠替換「xxx」呢

strings=['abc','xxx','ksl','xxx']

index=0

for string in strings:

if 'xxx' in string:

strings[index]='[censored]'

index=index+1

print(strings)

源代碼(注意源代碼的縮進)

這個程序能夠替換"xxx"的根本原因是變數index的自增和for循環的取列表strings中的元素string是同步的,

再加上if語句的判斷,所以能夠准確的替換"xxx",而不會將列表strings中的元素全部替換.

(你可能將if語句中的string字元串變數看成了strings列表了,所以不對了)

這個程序的執行過程是

index==0,for循環從strings列表取出第一個元素'abc',string=='abc',if 'xxx' in string為假,index=index+1

index==1,for循環從strings列表取出第二個元素'xxx',string=='xxx',if 'xxx' in string為真,strings[1]='[censored]',index=index+1

index==2,for循環從strings列表取出第三個元素'ksl',string=='ksl',if 'xxx' in string為假,index=index+1

index==3,for循環從strings列表取出第四個元素'xxx',string=='xxx',if 'xxx' in string為真,strings[3]='[censored]',index=index+1

string列表遍歷完了,退出for循環,列印更改後的列表(只有列表的第2,4項被更改)

所以是上述運行結果

Ⅱ 如何使用shell或python進行變數替換

echo $test
這是PHP吧?你確定要用python處理?
思路:
讀取a文件內容 遍歷每行內容
以單引號分割字元串 得到設置的值
按照b文件格式拼接出c文件的內容
最後生成或覆蓋c文件

Ⅲ python 字元串替換 $

試試看將替換後生成的字元串再生成模板後再次替換


tmpl=string.Template(...)

string.Template(tmpl.safe_substitute(**kwg)).safe_substitute(**kwg)

#!/usr/bin/python
#encoding:utf-8
#
#filename:stringTemplateLearning.py
#author:TimWang
#date:June,2013

importstring

tmpl=string.Template("""
$app:
home:$home
data:$data
""")

kwgs=dict(
app="App_Proj",
home="/home/tim/project/$app",
data="$home/data",
)

context=tmpl.safe_substitute(kwgs)
while"$"incontext:
tmpl=string.Template(context)
context=tmpl.safe_substitute(kwgs)

printcontext

>python -u "stringTemplateLearning.py"

App_Proj:
home: /home/tim/project/App_Proj
data: /home/tim/project/App_Proj/data

>Exit code: 0 Time: 0.044

Ⅳ python如何替換指定位置的數據

python可以使用replace方法替換指定字元,根據關鍵字替換字元串內的所有內容。也可以自定義方法,用循環進行遍歷處理

Ⅳ 怎樣通過參數替換python腳本里的變數值

一般情況下都是通過import腳本,然後直接調用腳本里的函數,調用函數就可以直接傳遞參數;因為Python並不像C語言那樣有main函數。
import B(腳本名稱)
B.hello(參數A,參數B)

Ⅵ python中的變數替換怎麼使用

1. 使用連接符: +
world = "World"print "Hello " + world + " ! "

2. 使用佔位符來內插
world = "World"print "Hello %s !" % world

3. 使用函數
li = ['my','name','is','bob']mystr = ' '.join(li)print mystr

上面的語句中字元串是作為參數傳入的,可以直接用變數替換:
begin_date = '2012-04-06 00:00:00'end_date = '2012-04-06 23:59:59'select * from usb where time between to_date(begin_date,'YYYY-MM-DD HH24:MI:SS') and to_date(end_date,'YYYY-MM-DD HH24:MI:SS')

Ⅶ Python 怎樣用變數替換字元串

很簡單

只要把

Xpath.replace(Xpath[35:36],num1)

改成

Xpath.replace(Xpath[35:36],str(num1))

以下是replace方法的詳細說明

replace(...)
S.replace(old,new[,count])->string


oldreplacedbynew.Iftheoptionalargumentcountis
given,.

參數只能是str類型

Ⅷ 如何用Python來進行查詢和替換一個文本字元串

1、說明
可以使用find或者index來查詢字元串,可以使用replace函數來替換字元串。
2、示例
1)查詢
>>> 'abcdefg'.find('cde')
結果為2
'abcdefg'.find('acde')
結果為-1
'abcdefg'.index('cde')
結果為2
2)替換
'abcdefg'.replace('abc','cde')
結果為'cdedefg'
3、函數說明
1)find(...)
S.find(sub[, start[, end]]) -> int
返回S中找到substring sub的最低索引,使得sub包含在S [start:end]中。 可選的 參數start和end解釋為切片表示法。
失敗時返回-1。
2)index(...)
S.index(sub[, start[, end]]) -> int
與find函數類似,但是當未找到子字元串時引發ValueError。
3)replace(...)
S.replace(old, new[, count]) -> str
返回S的所有出現的子串的副本舊換新。 如果可選參數計數為給定,只有第一個計數出現被替換。

Ⅸ python的replace函數怎麼用

Python replace()方法把字元串中的old(舊字元串)替換成new(新字元串),如果指定三個參數max,則替換不超過max次。

語法

replace()方法語法:

str.replace(old, new[, max])

參數

old -- 將被替換的子字元串;

new -- 新字元串,用於替換old子字元串;

max -- 可選字元串,替換不超過max次。

返回值

返回字元串中的old(舊字元串)替換成new(新字元串)後生成的新字元串,如果指定第三個參數max,則替換不超過max次。

實例

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";

print str.replace("is", "was");

print str.replace("is", "was", 3);

輸出結果

thwas was string example....wow!!! thwas was really string

thwas was string example....wow!!! thwas is really string

Ⅹ python,怎麼替換丟有變數的信息

# -*- coding: cp936 -*-
import re

p = re.compile(r'<a href="\?stypes=通用名\s*&key=.*">')
s = '<a href="?stypes=通用名&key=妥洛特羅貼劑">妥洛特羅貼劑'
print p.sub('', s)

# >>>
# 妥洛特羅貼劑

閱讀全文

與python變數替換相關的資料

熱點內容
二次元表格編程 瀏覽:20
plc編程器保停 瀏覽:963
如何降低伺服器的內存佔用率 瀏覽:868
阿里雲伺服器是個什麼意思 瀏覽:817
國內最好的數控編程培訓學校 瀏覽:13
222乘104列是演算法 瀏覽:159
程序員溝通正確姿勢 瀏覽:969
魔玩app怎麼視頻推廣 瀏覽:960
程序員抽獎送禮 瀏覽:458
北京java程序員薪資 瀏覽:658
如何創建網路平台或者app 瀏覽:355
python隨機數生成控制概率 瀏覽:235
壓縮機並聯運行 瀏覽:899
兩位單片機 瀏覽:63
四川音樂類投檔線的演算法 瀏覽:650
建行app如何改轉賬卡 瀏覽:26
android開發升級 瀏覽:299
要火社區app進不去怎麼辦 瀏覽:826
安卓手機上的自定義功能怎麼用 瀏覽:230
方舟伺服器怎麼購買進去資格 瀏覽:44