read_file = pd.read_csv('#f"{name}".csv')
That's not actually an f-string, it's a normal string that has an f
, some quotes, and some braces inside it. An f-string must be prefixed with (surprisingly enough) an f
:-)
I suggest you try something like:
read_file = pd.read_csv(f"#{name}.csv")
If the name
string holds xyzzy
, this will try to process the file #xyzzy.csv
.
Additionally, it's a very bad idea to use built-in functions as variable names. The input
function is one such example. The following (complete) snippet solves both those issues:
# Assumes file_base is a string, see below.
def cleaning(file_base):
read_file = pd.read_csv(f"#{file_base}.csv")
print(read_file)
cleaning("someString")
Note that you can put arbitrary expressions inside f-strings, not just variable names. Hence there's no need to create name
as a separate (named) object, you could just insert str(file_base)
inside the braces.
However, for your particular case, I don't think the use of str(file_base}
is necessary since it appears from tour comments that file_base
is already a string, and I believe the formatting for a string without a format_spec
is just the string itself(a).
This is why I have f"#{file_base}.csv"
in the code above without a str()
around the file_base
variable.
(a) For objects in general, an {object}
snippet inside an f-string will call the object's __format__()
method, passing in the format_spec
used. Hence f"{something:314159}
would use the object method call something.__format__("314159")
to format the object into a string.