excel_name.split('_')[0] how to only use the second '_'
时间: 2024-01-21 09:04:50 浏览: 17
You can use the `str.find()` function to find the index of the second underscore and then use slicing to extract the substring after it. Here's an example:
```
excel_name = "file_name_2022_01_31.xlsx"
second_underscore_index = excel_name.find('_', excel_name.find('_') + 1)
result = excel_name[second_underscore_index + 1:]
print(result) # Output: "2022_01_31.xlsx"
```
In this example, `find('_')` finds the index of the first underscore, and `find('_', find('_') + 1)` finds the index of the second underscore by starting the search from the index immediately after the first underscore. Then we use slicing to extract the substring after the second underscore.
相关问题
folder_name.split('_')[-1]啥意思
`folder_name.split('_')` 是将 `folder_name` 字符串按照下划线 `_` 进行分割,返回分割后的一个字符串列表。例如,如果 `folder_name` 的值为 `"abc_123"`,那么 `folder_name.split('_')` 的结果就是 `['abc', '123']`。
`[-1]` 表示获取列表中的最后一个元素,因为我们知道文件夹名称中最后一个下划线后面跟的是数字,所以我们使用 `[-1]` 来获取数字部分。在上面的示例代码中,我们通过 `int(folder_name.split('_')[-1])` 来获取文件夹名称中数字的值,然后将其作为排序规则的关键字。
db_full_name = self.config.table_full_name.split(".")[0]
这是一段 Python 代码,其中 self.config.table_full_name 是一个字符串,它可能是形如 "database_name.table_name" 的字符串。这段代码使用字符串的 split() 方法将该字符串按照 "." 进行分割,得到一个列表,其中第一个元素是数据库名,第二个元素是表名。然后将列表中的第一个元素赋值给变量 db_full_name。这段代码的作用是获取数据库名,并将其赋值给 db_full_name 变量。