-1

I have some .csv files that I read into my code that could have a number of different names.

ex.

17159_output.csv 18001_output.csv

I read it in with pandas

d = pd.read_csv('17159_output.csv')

How can I write it so that it is something like %%%%%_output.csv

d = pd.read_csv('%%%%%_output.csv')

%%%%% will always be 5 digits

geojasm
  • 121
  • 9
  • Do you know what the number is, or do you just want to read any file with that pattern? What if there are several? – Alex Hall Apr 19 '18 at 20:20
  • you can iterate files in the directory with `os.listdir(directory)` and then concatenate the result – peremeykin Apr 19 '18 at 20:21
  • There will only be one and it will have that pattern so I want to read anything with that pattern – geojasm Apr 19 '18 at 20:24
  • 1
    See this [answer](https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory?answertab=active#tab-top) – ALollz Apr 19 '18 at 20:40
  • Are you thinking you want to use regex? Are those `%` always going to be digits (numbers)? If you know there will only be one file that has a pattern, why do you want to read anything with that pattern since there's only going to be one? – Jarad Apr 19 '18 at 20:52
  • Because I am going to make this an executable and it will be run in different directories. The file will always have that pattern but the 5 digits will be different. – geojasm Apr 19 '18 at 21:02

1 Answers1

1

Just use simple string formatting for this.

d = pd.read_csv('{0}_output.csv'.format(17159)
MrLeeh
  • 5,321
  • 6
  • 33
  • 51