0
def cleaning(input):
    name = str(input)
    read_file = pd.read_csv('#f"{name}".csv')
    print(read_file)

cleaning(InputKeyword)

My function must take an input keyword, and this will change the "name" to "#input" word. I was trying with f-strigs, but it doesn't work. Any ideas or referrals where can I find the solution? Thanks in advance:)

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    It's not good form to change the very nature of your question once you have answers, as it invalidates them. Rolled back. If you adopt my solution and there's a *different* problem that should really be a different question. – paxdiablo Jul 11 '21 at 01:50
  • Try using `name = input()` and `pd.read_csv(f'#{name}.csv')`. – martineau Jul 11 '21 at 02:00

1 Answers1

4
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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Yes, thank you, I fixed the code. And yes, the name of the file is #name.csv –  Jul 11 '21 at 01:50
  • The function still does not work. NameError: name 'input' is not defined. –  Jul 11 '21 at 01:51
  • user16133525: [`input`](https://docs.python.org/3/library/functions.html#input) is the name of a built-in function and `str(input)` → `''`. – martineau Jul 11 '21 at 01:56
  • @user16133525: see the second part of my answer, `input` is a Python built-in function that you should not be using. – paxdiablo Jul 11 '21 at 01:57
  • Oh great, thank you! The only thing, I need to do cleaning(InputKeyword="mykeyword") to make it work –  Jul 11 '21 at 01:59
  • @user16133525, keep in mind that `cleaning(InputKeyword="mykeyword")` will set the `InputKeyword` parameter to `"mykeyword"`, meaning you'll need `def cleaning(InputKeyword)` - the names must match if you use *named* parameters. Or you could simply pass the *positional* parameter `"mykeyword"`. – paxdiablo Jul 11 '21 at 02:02
  • 1
    Is it even necessary to use `str(file_base)`? Won't the f-string formatter automatically call `str` for you if necessary? – Mark Ransom Jul 11 '21 at 02:07
  • Good point, @Mark, I just blindly copied in the expression from the OP. Have adjusted answer to add that in. – paxdiablo Jul 11 '21 at 02:20
  • And a nice adjustment it was too. I like the way you expanded on my comment. – Mark Ransom Jul 11 '21 at 02:28
  • For `{obj}`'s evaluation in an f-string, first `type(obj).__format__(obj, "")` is tried then it falls back to `__str__` and then `__repr__`. For a string `obj`, first two give the same result but your last paragraph seems to be mentioning a generic `obj` so I thought this might be worth mentioning... – Mustafa Aydın Jul 11 '21 at 09:09
  • @MustafaAydın, thanks greatly for the education, I'm almost always open to making improvements based on suggestions. I've adjusted the answer to incorporate your comments, feel free to educate me even more if you still see any shortcomings. – paxdiablo Jul 12 '21 at 03:26
  • Oh, who am I to educate, thanks for the answer. – Mustafa Aydın Jul 12 '21 at 19:25