-1

I'm gettin this error: print("=" * 20) ^ SyntaxError: EOF while scanning triple-quoted string literal and it's indicating what it seems to be a random place in the code

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)
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Berny
  • 113
  • 11

2 Answers2

0

using a triple quote is unnecessary here. use an r'' string to prevent escape sequences from happening

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)
  • [This won't work either](https://stackoverflow.com/q/2870730/354577), as the syntax highlighting here should hint. Raw strings with trailing backslashes don't work without some awkward gymnastics. – ChrisGPT was on strike Jan 04 '21 at 17:53
-2

You are using the triple quote.

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

Triple Quote is used for comments and that comment consumes whole row in the code i.e. code and comment in triple quote can't be written in a single line.

So, try to use this:

data_location = "C:\\Users\\bosuna\\OneDrive\\Desktop\\pure_b2b\\"
yashcode17
  • 12
  • 4
  • 1
    triple-quote is very valid for string literals: https://docs.python.org/3/tutorial/introduction.html#strings – trichner Jan 04 '21 at 18:37
  • Triple quotes are _not_ for comments. Hashes are for comments. Triple quotes are for multiline string literals, and string literals can be used for documentation in some places. But they're not comments. – ChrisGPT was on strike Jan 05 '21 at 12:59
  • This works because of the _double backslashes_, not the quotes. Triple quotes vs. single quotes is entirely irrelevant: `"""C:\\Users\\bosuna\\OneDrive\\Desktop\\pure_b2b\\""" == "C:\\Users\\bosuna\\OneDrive\\Desktop\\pure_b2b\\"` → `True`. Triple-quoted strings definitely _can_ be written on a single line. – ChrisGPT was on strike Jan 05 '21 at 15:40