Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am i getting SyntaxError: EOL while scanning string literal in Python?

# April sales data
df = pd.read_csv(r'C:\Users\joseph chang\OneDrive\Tax documents\Programming\Python\Pandas-Data-Science-Tasks-master\SalesAnalysis\Sales_Data\Sales_April_2019.csv')

# All sales data
files = [file for file in os.listdir(r'C:\Users\joseph chang\OneDrive\Tax documents\Programming\Python\Pandas-Data-Science-Tasks-master\SalesAnalysis\Sales_Data')]
                                     
#for file in files:
    #print(file)
                                     
#df.head()

#empty df to sore all data
all_months_data = pd.DataFrame()

#concatenate the data into a single df
for file in files:
    df = pd.read_csv(r'C:\Users\joseph chang\OneDrive\Tax documents\Programming\Python\Pandas-Data-Science-Tasks-master\SalesAnalysis\Sales_Data\'+file)
    all_months_data = pd.concat([all_months_data, df])

all_months_data.head()

>Solution :

If you take a close look in an IDE (or stackoverflows code highlighter) you will notice that you have a string that isn’t ending. Python interpreter was expecting the a ‘ character to end the string, but instead found an EOL character representing the end of the file.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The reason your ‘ character wasn’t picked up was because a backslash is before it. the backslash is an escape character in python, meaning it has special qualities when read by the interpreter. To represent the backslash character, use TWO backslashes. I have adjusted your code below by changing your backslashes, it should work now.

# April sales data
df = pd.read_csv('C:\\Users\\joseph chang\\OneDrive\\Tax documents\\Programming\\Python\\Pandas-Data-Science-Tasks-master\\SalesAnalysis\\Sales_Data\\Sales_April_2019.csv')

# All sales data
files = [file for file in os.listdir('C:\\Users\\joseph chang\\OneDrive\\Tax documents\\Programming\\Python\\Pandas-Data-Science-Tasks-master\\SalesAnalysis\\Sales_Data')]
                                     
#for file in files:
    #print(file)
                                     
#df.head()

#empty df to sore all data
all_months_data = pd.DataFrame()

#concatenate the data into a single df
for file in files:
    df = pd.read_csv('C:\\Users\\joseph chang\\OneDrive\\Tax documents\\Programming\\Python\\Pandas-Data-Science-Tasks-master\\SalesAnalysis\\Sales_Data\\'+file)
    all_months_data = pd.concat([all_months_data, df])

all_months_data.head()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading