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
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.
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', \
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))

Possibly Related Threads…
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 467 Sep-18-2020, 10:10 PM
Last Post: tienttt
  Iterating over word vs. character Mark17 6 290 Aug-11-2020, 04:56 PM
Last Post: Mark17
  Remove from end of string up to and including some character lbtdne 2 446 May-17-2020, 09:24 AM
Last Post: menator01
  Help ANSI character and python... sid4g 2 402 Mar-19-2020, 06:08 PM
Last Post: sid4g
  loop only prints last character. mcmxl22 1 366 Feb-17-2020, 02:36 AM
Last Post: menator01
  create a list of object with a list of character studenthch 0 315 Feb-12-2020, 08:43 AM
Last Post: studenthch
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 14,493 Feb-08-2020, 06:58 PM
Last Post: karkas
  convert a character to numeric and back Skaperen 2 433 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  Regex won't replace character with line break Tomf96 2 494 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 547 Jan-05-2020, 11:50 AM
Last Post: vivekagrey

Forum Jump:

User Panel Messages

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