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
  width of Unicode character Skaperen 6 589 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  How to do line continuation in Jupyter Notebook? Mark17 4 790 Sep-22-2021, 04:22 PM
Last Post: ibreeden
  Gravity doesn't works, and my character just moves right and really fast izmamonke 1 496 Aug-08-2021, 02:55 AM
Last Post: deanhystad
Star I was making code for collision for my character izmamonke 2 635 Aug-06-2021, 04:30 PM
Last Post: izmamonke
  Regex: a string does not starts and ends with the same character Melcu54 5 866 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 900 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  python error: bad character range \|-t at position 12 faustineaiden 0 788 May-28-2021, 09:38 AM
Last Post: faustineaiden
  fpdf star character issue KatMac 3 1,037 May-01-2021, 06:22 PM
Last Post: KatMac
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 935 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
  Character Increment AnokhiRaaz 1 1,046 Apr-22-2021, 04:29 AM
Last Post: buran

Forum Jump:

User Panel Messages

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