We help IT Professionals succeed at work.

Python - adding variable inside a string

csehz
csehz asked
on
175 Views
Last Modified: 2018-08-29
Dear Experts,

Actually having the following Python code to import .csv file and it works fine:

import csv

with open(r"C:\File.csv") as csvfile:
    rows = list(csv.reader(csvfile))
    data_header = rows[0]
    data_rows = rows[1:]
    

Open in new window


I would like to restructure a little bit with adding the path to a variable as FilePath and using that in the open command, around like this (it does not work of course in this format)

import csv

FilePath = "C:\File.csv"

with open(r % FilePath) as csvfile:
    rows = list(csv.reader(csvfile))
    data_header = rows[0]
    data_rows = rows[1:]
    

Open in new window


Could you please advice about the Python syntax including a variable like that?

Thanks in advance,
Comment
Watch Question

Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
This should work...

with open(FilePath) as csvfile:

Open in new window

csehzIT consultant

Author

Commented:
@Neeraj thanks for the advice, with that syntax getting the following error message:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Presumably that 'r' sign should be somehow added at the beginning as it is in the format of

with open(r"C:\File.csv") as csvfile:

Open in new window

Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
csehzIT consultant

Author

Commented:
@pepr thanks a lot for the explanation, it works of course like that
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
Didn't I offer the same solution? :)
csehzIT consultant

Author

Commented:
@Neeraj oops you are right just when came in the morning did not catch that you also answered :(

Sorry again as you again did not get the well deserved points (as we spoke I caused that you did not got all the 1000 in the other thread) :(
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
Never mind. :)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.