Reputation: 31
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
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
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()
Upvotes: 2
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