Python: Syntaxerror: Eol While Scanning String Literal

python: SyntaxError: EOL while scanning string literal

You are not putting a " before the end of the line.

Use """ if you want to do this:

""" a very long string ...... 
....that can span multiple lines
"""

Python Syntax Error EOL while scanning string literal - why it happens and how to fix it?

The error you get:

SyntaxError: EOL while scanning string literal

Is a syntax error, because that the last line is malformed.

The closing " of the string format is missing.

Change:

print("String Tokens: {}\nNumeric Tokens: {}.format(len(str_tokens), len(num_tokens)))

To:

print("String Tokens: {}\nNumeric Tokens: {}".format(len(str_tokens), len(num_tokens)))

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

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.

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()

SyntaxError: EOL while scanning string literal while doing string interpolation for file names

Backslashes are used to form special characters like \n and to escape characters that would have an effect, like in your case the quote sign. \" is going to be a literal quote sign in the string and it will not terminate the string. To change that behavior of backslashes, you need to escape them themselves. I.e., if you want a backslash in a string, always type \\ instead of \.

a.to_csv("~\\Desktop\\" + file.split('\\')[-1])

should work.

SyntaxError: EOL while scanning string literal in Python

You cannot split a long line into multiple lines hitting enter. Either change your element= line to a single line like this

element = '("' +train_data.text[i] + '"' + ', {"entities": [(' + str(a.start()) + ',' + str(a.end()) + ',"SKILL")]})'

or add a \ at the end of the line

element = '("' +train_data.text[i] + '"' + ', {"entities": [(' + \
str(a.start()) + ',' + str(a.end()) + ',"SKILL")]})'

Backslash error SyntaxError: EOL while scanning string literal

The error is simple. Just had the same
Use this:

fileName = "file"
linkName = "rep" + chr(92) + file_name
print(linkName)

chr(92) stands for \

python: SyntaxError: EOL while scanning string literal

You are not putting a " before the end of the line.

Use """ if you want to do this:

""" a very long string ...... 
....that can span multiple lines
"""


Related Topics



Leave a reply



Submit