#Python TQC考題906 字串資料取代
“””
以下為視訊教學
最後一行錯了!
正確應為:
file.write(data.replace(s1,s2))
“””

“””
read.txt內容:
watch shoes skirt
pen trunks pants
“””
fname=input()
strold=input()
strnew=input()
with open(fname,”r+“,encoding=”utf-8”) as file:
# fname是input()進來,不需要” ” , r+可讀寫
#若將r+改成w+,replace前後都是空的
data=file.read()
print(“=== Before the replacement”)
print(data)
print(“=== After the replacement”)
print(data.replace(strold,strnew))
file.seek(0) #回到檔案起點
#少這一行的話,位置於文件之末,
# 會於文件末再多一段資料
#而非蓋掉原資料
file.write(data)
“””
這一行應該是錯的,
data.txt將不會被新資料寫入
應該修正為:
file.write(data.replace(strold,strnew))
這樣才把新資料寫入data.txt
“””

“””
datanew = data.replace(strold,strnew)
print(datanew)
要用datanew承接data.replace(strold,strnew)
再印datanew
印data的話,仍是舊資料
以下有成功將sneakers寫入data.txt,
注意: f.write(datanew) ,
不是f.write(data)
刪除錯好幾次的程式碼
data.txt沒有改變的話
就是錯這個地方
“””

#再練習一次

#再練習一次:

![Python: 如何用numpy.ndarray的reshape 將3D array轉為2D array,再轉為pandas.DataFrame? arr.reshape( arr.shape[0] * arr.shape[1] , -1) Python: 如何用numpy.ndarray的reshape 將3D array轉為2D array,再轉為pandas.DataFrame? arr.reshape( arr.shape[0] * arr.shape[1] , -1)](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230320082325_85.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python 如何用pandas.Series.nsmallest() 找到n個與target差距最小的index?再從中找到距離idxmax最近的index?避免誤抓sidelobes的index? targetIdx = (serMean-target_value).abs().nsmallest(n).index.tolist() ;Series切片: .loc[標籤名1:標籤名2] (會含標籤名2) ; .iloc[位置1:位置2] (不含位置2) Python 如何用pandas.Series.nsmallest() 找到n個與target差距最小的index?再從中找到距離idxmax最近的index?避免誤抓sidelobes的index? targetIdx = (serMean-target_value).abs().nsmallest(n).index.tolist() ;Series切片: .loc[標籤名1:標籤名2] (會含標籤名2) ; .iloc[位置1:位置2] (不含位置2)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230222082954_53.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


![Python: pandas.DataFrame如何移除所有空白列?pandas.isna( df_raw[0] ).tolist() ; df_drop0 = df_raw.drop(nanIdx,axis=0).reset_index(drop=True) Python: pandas.DataFrame如何移除所有空白列?pandas.isna( df_raw[0] ).tolist() ; df_drop0 = df_raw.drop(nanIdx,axis=0).reset_index(drop=True)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/12/20221206184635_4.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言