python提取第一列中的“币种:"字段后面的中文
时间: 2024-11-16 19:19:47 浏览: 47
在Python中,要从MySQL数据库的查询结果中提取特定字段(如"币种:"后面的中文)并处理包含新外币的情况,可以按照以下步骤进行:
1. 使用`pymysql`库连接到数据库[^1],这里假设`etl1_58infor_data`表已存在并且`field_list`包含了包含"币种:"字段的列名。
```python
import pymysql
# 创建连接
project2 = Mysql('etl1_58infor_data', field_list=field_list, field_num=26, conn_type='local')
# 执行SQL查询,获取包含"币种:"字段的数据
cursor = project2.execute_query('SELECT * FROM your_table WHERE column_with_currency LIKE "%币种:%"')
# 遍历查询结果
for row in cursor.fetchall():
currency_column = row[field_list.index('column_with_currency')] # 获取包含"币种:"的列
if "币种:" in currency_column:
# 提取"币种:"后的中文部分
chinese_currency = currency_column.split("币种: ")[1].strip() # 剔除首尾空白
print(f"币种: {chinese_currency}")
# 检查是否有新外币出现
new_currency = ... # 根据实际条件判断新币种
if new_currency and not chinese_currency == new_currency:
# 更新数据库
update_query = f"UPDATE your_table SET column_with_currency = REPLACE(column_with_currency, '{chinese_currency}', '{new_currency}') WHERE column_with_currency LIKE '%{new_currency}%'"
project2.execute(update_query)
```
请注意,`your_table`, `column_with_currency`, 和 `new_currency`需要替换为实际表名和字段名,以及新的外币值。执行更新操作时要谨慎,确保只有在确实发现新币种的情况下才执行更新。
阅读全文
相关推荐


















