filename = "14.5GHZ.csv"
parts = filename.rsplit(".", 1)
print(parts)使用Python的rsplit()方法,并设置maxsplit参数为1,这样它只会从右侧拆分一次。这样可以确保小数点不被作为拆分点。
輸出結果:

使用正則表示法
import re
filename = "14.5GHZ.csv"
parts = re.split(r"\.(?!\d)", filename)
print(parts)輸出結果:

使用正则表达式模式\.(?!\d)进行拆分,该模式匹配一个点(.)后面不跟随数字的情况。这样,文件名 “14.5GHZ.csv” 就被拆分为两部分:[‘14.5GHZ’, ‘csv’]。正则表达式模式中的(?!\d)是一个负向前瞻断言,表示后面不应该跟随数字。
使用正则表达式进行拆分可以提供更灵活的拆分规则,以适应各种情况。但需要注意正则表达式的编写和匹配规则,确保满足您的拆分需求。
使用os模組:
os.path.splitext(filename)輸出:

推薦hahow線上學習python: https://igrape.net/30afN
![Python 如何做excel的樞紐分析? pandas.pivot_table() 或 pandas.DataFrame .groupby() ; 如何指定欄位順序? DataFrame.reindex() ; .sortlevel() ; DataFrame[[col1, col2, col3 ]] ; df.columns.map() ; 如何顯示所有欄? pandas.set_option (“display.max_columns”, None) Python 如何做excel的樞紐分析? pandas.pivot_table() 或 pandas.DataFrame .groupby() ; 如何指定欄位順序? DataFrame.reindex() ; .sortlevel() ; DataFrame[[col1, col2, col3 ]] ; df.columns.map() ; 如何顯示所有欄? pandas.set_option (“display.max_columns”, None)](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230323101848_80.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://i0.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230222082954_53.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


![Python提取2D array的一部份資料; import numpy; a[1: , 2:] ; a[1:-1 , 2:-1] Python提取2D array的一部份資料; import numpy; a[1: , 2:] ; a[1:-1 , 2:-1]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/09/20220906122441_97.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言