Python "" 问题

Python "" issue

我遇到了这个错误: 打印("=" * 20) ^ SyntaxError: EOF while scanning triple-quoted string literal 它表明它似乎是代码中的随机位置

import os
import numpy as np
import pandas as pd

data_initial = "FY21 DCG NA Campaign KPI Dashboard.xlsx"
output = "pure_b2b_summary.xlsx"
data_location = """C:\Users\bosuna\OneDrive\Desktop\pure_b2b\"""
col_list = ["Email", "SOURCE"]
df_total = pd.DataFrame(columns=col_list)

df_initial = pd.read_excel(data_initial, engine='openpyxl')
for root, dirs, files in os.walk(data_location):
    for file in files:
        if file.endswith(".csv"):
            df = pd.read_csv(data_location + file, encoding='latin-1', usecols=col_list)
            df_total = pd.concat([df, df_total], ignore_index=True)

df_initial.rename(columns={"Source Code": "SOURCE", "5. Campaign Type": "Campaign"}, inplace=True)
df_merge = pd.merge(df_total, df_initial[["SOURCE", "Campaign"]], on="SOURCE", how='left')
df_merge.to_excel(output, index=False)

print("=" * 20)
print("Unique Leads:", df_total.groupby('SOURCE')['Email'].nunique())
# print("Unique Leads:", df_merge.groupby('Campaign')['Email'].nunique())
print("=" * 20)
print("Output has been exported")
print("=" * 20)

这里不需要使用三重引号。使用 r'' 字符串防止转义序列发生

import os
import numpy as np
import pandas as pd

data_initial = "FY21 DCG NA Campaign KPI Dashboard.xlsx"
output = "pure_b2b_summary.xlsx"
data_location = r"C:\Users\bosuna\OneDrive\Desktop\pure_b2b\"
col_list = ["Email", "SOURCE"]
df_total = pd.DataFrame(columns=col_list)

df_initial = pd.read_excel(data_initial, engine='openpyxl')
for root, dirs, files in os.walk(data_location):
    for file in files:
        if file.endswith(".csv"):
            df = pd.read_csv(data_location + file, encoding='latin-1', usecols=col_list)
            df_total = pd.concat([df, df_total], ignore_index=True)

df_initial.rename(columns={"Source Code": "SOURCE", "5. Campaign Type": "Campaign"}, inplace=True)
df_merge = pd.merge(df_total, df_initial[["SOURCE", "Campaign"]], on="SOURCE", how='left')
df_merge.to_excel(output, index=False)

print("=" * 20)
print("Unique Leads:", df_total.groupby('SOURCE')['Email'].nunique())
# print("Unique Leads:", df_merge.groupby('Campaign')['Email'].nunique())
print("=" * 20)
print("Output has been exported")
print("=" * 20)

您正在使用三重引号。

data_location = """C:\Users\bosuna\OneDrive\Desktop\pure_b2b\"""

三重引号用于 评论 并且该评论占用代码中的整行,即三重引号中的代码和评论不能写在一行中。

所以,尝试使用这个:

data_location = "C:\Users\bosuna\OneDrive\Desktop\pure_b2b\"