read in one row of csv file (based on input if i can) with DictReader, then format and write to new file
Question: I'm trying to read in a csv file with many rows and columns; i would like to
print one row, in a particular format, to a text file, and do some hashing on
the values. SO far, i have been able to read in the file, parse thru it using
DictReader, find the row i want using an IF statement and then print the keys
and values. I cannot figure out how to format it to the format i want in the
end ( Key = Value \n), and i cannot figure how to write to a file (much less
in the format i want) using the value of 'row' obtained below. I've been
trying for days and make a little progress but cannot get it to work. Here is
what i got to work (with much detail left out of results):

* * *
    
    
    >>>import csv
    
    with open("C:\path_to_script\filename_Brief.csv") as infh:
        reader = csv.DictReader(infh)
        for row in reader:
            if row['ALIAS'] == 'Y4K':
                print(row)
    

# result-output

{'Full_Name': 'Jack Flash', 'PHONE_NO': '555 555-1212', 'ALIAS': 'Y4K'}

* * *

I'd like to ask the user to input the Alias and then use that to determine row
to print. I've done a ton of research but am new-ish to Python so am asking
for help! i've used pyexcel, xlrd/xlwt, even thought I'd try pandas but too
much to learn. I also got it to format the way i wanted in one test but then
could not get the row selection to work--in other words, it prints all the
records rather than the row i want. Have 30 Firefox tabs open trying to find
an answer! Thanks in advance!


Answer: The following may at least be close to what you want (I think):

    
    
    import csv
    
    with open(r'C:\path_to_script\filename_Brief.csv') as infh, \
         open('new_file.txt', 'wt') as outfh:
        reader = csv.DictReader(infh)
        for row in reader:
            if row['ALIAS'] == 'Y4K':
                 outfh.write('Full_Name = {Full_Name}\n'
                             'PHONE_NO = {PHONE_NO}\n'
                             'ALIAS = {ALIAS}\n'.format(**row))
    

This would write 3 lines formatted like this into the output file for every
matching`row`:

    
    
    Full_Name = Jack Flash 
    PHONE_NO = 555 555-1212
    ALIAS = Y4K            
    

BTW, the `**row`notation means basically "take all the entries in the
specified dictionary and turn them into keyword arguments for this function
call". The `{keyword}` syntax in the format string refers to any keyword
arguments that will be passed to the `str.format()` method.

