创建一个空的DataFrame df = pd.DataFrame(columns=['总订单数','总推送数','推送比','问卷回馈','回馈比']) count = 0 for i in range(5): for message in messages: if "20230612统计" in message.Subject: 使用正则表达式提取订单数、推送数和回馈数的值 pattern1 = r"总订单数:(\d+)" pattern2 = r"总推送数:(\d+)" pattern3 = r"推送比:(\d+)" pattern4 = r"问卷回馈:(\d+)" pattern5 = r"回馈比:(\d+)" match1 = re.search(pattern1, message.Body) match2 = re.search(pattern2, message.Body) match3 = re.search(pattern3, message.Body) match4 = re.search(pattern4, message.Body) match5 = re.search(pattern5, message.Body) if match1 and match2 and match3 and match4 and match5: 总订单数 = match1.group(1) 总推送数 = match2.group(1) 推送比 = match3.group(1) 问卷回馈 = match4.group(1) 回馈比 = match5.group(1) print("总订单数::", 总订单数) print("总推送数:", 总推送数) print("推送比", 推送比) print("问卷回馈:", 问卷回馈) print("回馈比", 回馈比) else: print("帅哥") count += 1 if count == 1: break else: continue break df.loc[count] = ['20230612统计', message.Subject, 总订单数, 总推送数, 推送比, 问卷回馈, 回馈比] 保存DataFrame到Excel文件 df.to_excel(r'C:\Users\bjbk0026\Desktop\统计.xlsx', index=False) 推送比和回馈比设置为默认为空怎么写
时间: 2024-04-16 17:27:02 浏览: 19
如果你想在推送比和回馈比未匹配到值时将其设置为空,默认为空字符串`''`,你可以在条件判断中处理这种情况。以下是修改后的代码示例:
```python
df = pd.DataFrame(columns=['总订单数','总推送数','推送比','问卷回馈','回馈比'])
count = 0
for i in range(5):
for message in messages:
if "20230612统计" in message.Subject:
pattern1 = r"总订单数:(\d+)"
pattern2 = r"总推送数:(\d+)"
pattern3 = r"推送比:(\d+)"
pattern4 = r"问卷回馈:(\d+)"
pattern5 = r"回馈比:(\d+)"
match1 = re.search(pattern1, message.Body)
match2 = re.search(pattern2, message.Body)
match3 = re.search(pattern3, message.Body)
match4 = re.search(pattern4, message.Body)
match5 = re.search(pattern5, message.Body)
if match1 and match2 and match4:
总订单数 = match1.group(1)
总推送数 = match2.group(1)
问卷回馈 = match4.group(1)
if match3:
推送比 = match3.group(1)
else:
推送比 = '' # 设置默认为空字符串 ''
if match5:
回馈比 = match5.group(1)
else:
回馈比 = '' # 设置默认为空字符串 ''
print("总订单数::", 总订单数)
print("总推送数:", 总推送数)
print("推送比", 推送比)
print("问卷回馈:", 问卷回馈)
print("回馈比", 回馈比)
else:
print("帅哥")
count += 1
if count == 1:
break
else:
continue
break
df.loc[count] = ['20230612统计', message.Subject, 总订单数, 总推送数, 推送比, 问卷回馈, 回馈比]
df.to_excel(r'C:\Users\bjbk0026\Desktop\统计.xlsx', index=False)
```
在上述代码中,我在匹配`推送比`和`回馈比`时,使用了条件判断来处理未匹配到值的情况。如果未匹配到值,我将其设置为空字符串`''`作为默认值。然后将提取到的值添加到DataFrame中,并将DataFrame保存到Excel文件中。
希望这能满足你的需求!如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)