Python - adding variable inside a string

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,
LVL 1
csehzIT consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
This should work...

with open(FilePath) as csvfile:

Open in new window

0
csehzIT consultantAuthor 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

0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Did you try FilePath = r"C:\File.csv"?
1
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

peprCommented:
It should be
import csv

FilePath = r"C:\File.csv"    # the r should be here

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

Open in new window

The r % FilePath does not make sense here. The r in r'string literal' is just a string literal prefix that says that the content should not be interpreted. It means that you need not prefix the backslash (to double it). The r is from raw string. It is similar (but not the same) as @"string literal" in C# (or the like special sequence in C++ or in other languages). Alternatively, you can accept the Unix path style with normal slashes, like FilePath = 'C:/File.csv'

You should also read the Style Guide for Python concering the naming conventions. I understand that they may be different in other languages, but it will help you to understand Python code in long term. ;)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
csehzIT consultantAuthor Commented:
@pepr thanks a lot for the explanation, it works of course like that
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Didn't I offer the same solution? :)
0
csehzIT consultantAuthor 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) :(
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Never mind. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.