Python Forum
SyntaxError: unexpected character after line continuation character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: unexpected character after line continuation character
#1
Hi!
I want to run the under mentioned code:

import csv

%precision 2

with open(C\Users\LENOVO\Documents\'mpg.csv') as csvfile:
mpg = list(csv.DictReader(csvfile))

But I am getting the error as:
File "<ipython-input-5-e14b7d279de2>", line 5
with open(C\Users\LENOVO\Documents\'mpg.csv') as csvfile:
^
SyntaxError: unexpected character after line continuation character

May kindly help.
Reply
#2
I get "invalid syntax" for "%precision 2", which is expected. What do you think was going on in that line?

The continuation character message is related to the path not being a string. Only the filename and extension are inside the single quotes. '\' is the line continuation character.
print('I think this is a very long line, \
      'that I want to break up into shorter', \
      'lines')
Reply
#3
Use code tag.
There are several problems here invalid Python syntax %precision 2,missing : and '.
Do not use single \ backslash in path,see raw string in code r''.
import csv

# %precision 2 ## Not a Python syntax

with open(r'C:\Users\LENOVO\Documents\mpg.csv') as csvfile:
    mpg = list(csv.DictReader(csvfile))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is any character in s1 in s2 Skaperen 6 1,204 Sep-07-2024, 12:18 AM
Last Post: Skaperen
  python escaping character atom509 1 714 May-29-2024, 08:57 PM
Last Post: DeaD_EyE
  Python rule about the space character surrounding the equal sign ineuw 10 4,499 Sep-21-2023, 09:17 AM
Last Post: ineuw
  How do I handle escape character in parameter arguments in Python? JKR 6 5,315 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 9,293 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  use of escape character in re.sub and find WJSwan 1 1,543 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 3,623 Sep-27-2022, 01:38 PM
Last Post: buran
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 28,857 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  Unsupported Format Character Led_Zeppelin 2 10,589 Sep-20-2022, 01:46 PM
Last Post: deanhystad
  how to read chinese character? kucingkembar 2 2,375 Aug-25-2022, 08:42 PM
Last Post: kucingkembar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020