user3553019
user3553019

Reputation: 31

EOL while scanning string literal

this is my code and get a following error message: line 8 sepFile=readFile.read().split('\') SyntaxError: EOL while scanning string literal could you help me? Thanks.

import matplotlib.pyplot as plt
import numpy as np
x=[]
y=[]

readFile = open (("/Users/Sun/Desktop/text58.txt"), 'r')

sepFile=readFile.read().split('\')
readFile.close()

For plotPair in sepFile:
    xANDy=plotPair.split(',')
    x.append(int(xAndy[2]))
    y.append(int(xAndy[1]))
print x
print y

plt.plot(x,y)

plt.title('tweet')
plt.xlabel('')
plt.ylabel('')
plt.show()

Upvotes: 3

Views: 10247

Answers (3)

user2555451
user2555451

Reputation:

\ is a special character in Python string literals: it starts an escape sequence.

To fix the problem, you need to double the backslash:

sepFile=readFile.read().split('\\')

Doing so tells Python that you are using a literal backslash rather than an escape sequence.


Also, for, like all keywords in Python, needs to be lowercase:

for plotPair in sepFile:

Upvotes: 6

A.J. Uppal
A.J. Uppal

Reputation: 19264

You cannot split by '\', it is used for special escape sequences such as '\n' and '\t'. Try a double backslash: '\\'. Here is your revised code:

import matplotlib.pyplot as plt
import numpy as np
x=[]
y=[]

readFile = open (("/Users/Sun/Desktop/text58.txt"), 'r')

sepFile=readFile.read().split('\\')
readFile.close()

For plotPair in sepFile:
    xANDy=plotPair.split(',')
    x.append(int(xAndy[2]))
    y.append(int(xAndy[1]))
print x
print y

plt.plot(x,y)

plt.title('tweet')
plt.xlabel('')
plt.ylabel('')
plt.show()

Look here for more information on backslashes

Upvotes: 2

Michael0x2a
Michael0x2a

Reputation: 63998

StackOverflow's syntax highlighting actually gives a good clue as to why this is happening!

The problem is with this line:

sepFile=readFile.read().split('\')

...or more specifically, this:

'\'

That backslash is escaping the last quote, so Python doesn't know that the string has ended. It'll keep going, but the string never terminates, so it throws an exception.

To fix it, escape the backslash:

sepFile=readFile.read().split('\\')

Upvotes: 0

Related Questions