-1

I am trying to read data and I cannot understand what the {0} should be doing when reading the file. What is the purpose of it?

data_files = [
    "ap_2010.csv",
    "class_size.csv",
    "demographics.csv",
    "graduation.csv",
    "hs_directory.csv",
    "sat_results.csv"
]
data = {}

for file in data_files:
    dataframe= pd.read_csv('schools/{0}'.format(file))
    dataframe_name = file.replace(".csv","")
    data[dataframe_name] = dataframe```
msanford
  • 11,803
  • 11
  • 66
  • 93
smn
  • 1
  • 1
  • 1
    See [str.format](https://docs.python.org/3/library/stdtypes.html#str.format). You are passing the 0th argument (`file` in your example) to `{0}` – not_speshal Jul 07 '21 at 17:57
  • 1
    Does this answer your question? [String formatting in Python](https://stackoverflow.com/questions/517355/string-formatting-in-python) – buran Jul 07 '21 at 17:57
  • 1
    also this is NOT proper way to construct path. you should use `pathlib` or `os.path.join()` – buran Jul 07 '21 at 17:59
  • 1
    Also, which python interpreter _are you actually using_: 2 or 3? (Don't use both tags 'just because'.) – msanford Jul 07 '21 at 17:59

2 Answers2

1

It's a way to link the list of values you're passing in the format() method to their place in the string. The {0} says it should be replaced by the first value on the format method. This means that you will end up with a 'schools/file'.

If you had another value like file format (csv, json, parquet) you could use:

"schools/{0}.{1}".format(file, format)

To get what you want.

0

It's a positional argument to str.format():

Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

msanford
  • 11,803
  • 11
  • 66
  • 93