How to parse a file, looking for a match to a command line argument?

I’m brand new to Python (still SO much to learn!) and working on my first project. I’d prefer you don’t respond with the code I need but rather with info that will tell me where to find the answer on my own.

I’m creating a script - ref - that I can use as a cheatsheet for various commands as I learn. I’m learning Linux commands, HTML, CSS, and Javascript, too, so I’ll have one reference file per language. To open (for example) the python reference file (pythonref.html - filled with notes I’ve added using my ref script), I want to be able to type ref p rather than ref python. I plan to use a csv file for these abbreviations:

p,python
l,linux
h,html
c,css

and I want my script to parse that file, comparing the first letter of each line to the first argument specified - and when it finds a match, it gets the 2nd part of the matched line and uses it to set the reffile var to the filename of the reference file. So if the arg passed is c, then the reffile var will be set to cssref.html. If the arg passed is p, then the reffile var will be set to pythonref.html, etc. To complicate things a bit, there may be 1 arg passed or there may be 2. The second arg is for an action - a to add an entry to the file, r to remove an entry from the file, etc. If there’s only 1 arg passed, then the default is to open the file.

What’s the right way to do this? I’ve looked at (and understand) how to parse the command args using sys.argv it’s parsing the csv file and getting that second value that has me stumped.

Thank you for your time and consideration!

Why do you want to store those abbreviations in a file, instead of in a data structure in your program (like a dictionary, for instance)?

By Don Ferris via Discussions on Python.org at 23Jul2022 23:44:

I’m brand new to Python (still SO much to learn!) and working on my
first project. I’d prefer you don’t respond with the code I need but
rather with info that will tell me where to find the answer on my own.

+1

I’m creating a script - ref - that I can use as a cheatsheet for various commands as I learn. I’m learning Linux commands, HTML, CSS, and Javascript, too, so I’ll have one reference file per language. To open (for example) the python reference file (pythonref.html - filled with notes I’ve added using my ref script), I want to be able to type ref p rather than ref python. I plan to use a csv file for these abbreviations:

p,python
l,linux
h,html
c,css

and I want my script to parse that file, comparing the first letter of each line to the first argument specified - and when it finds a match, it gets the 2nd part of the matched line and uses it to set the reffile var to the filename of the reference file. So if the arg passed is c, then the reffile var will be set to cssref.html. If the arg passed is p, then the reffile var will be set to pythonref.html, etc. To complicate things a bit, there may be 1 arg passed or there may be 2. The second arg is for an action - a to add an entry to the file, r to remove an entry from the file, etc. If there’s only 1 arg passed, then the default is to open the file.

What’s the right way to do this? I’ve looked at (and understand) how to
parse the command args using sys.argv it’s parsing the csv file and
getting that second value that has me stumped.

You want the csv module:

Likely process:

  • open your abbreviations file for read
  • make a csv.reader from the open file
  • use it to read rows from the file - they come back as a Python list,
    one list per line

Then for each row, compare it to your command line arg as desired.

You’ll discover that naively doing the above entwines your command line
stuff with the file read/parse. It is likely that your file is quite
small, so perhaps think about writing a function to read the whole file
and return a mapping of abbreviation to name (i.e. a dict).

Then your main program can go:

  • read the mapping from the file via the function
  • see if the command line arg is in the mapping

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks for the response, ND. I’m new enough to Python that I hadn’t yet learned about (even the existence of) dictionaries. I’ve looked it up and started to play with it and it seems like this is the logical way to go for my language abbreviations. Thank you!

Thanks, Cameron! I’m going to use a dictionary for this but I’ve started a second project where I think csv will be the way to go.