Print Lines from .csv based off criteria inputted from user

create csv file command line
awk print third column csv
awk print nth column csv
how to read csv file in command prompt
command line get column from csv
unix command csv file
bash csv
how to open csv file in command prompt

I am new to python and am looking for guidance on the task I am working on.

I am importing a csv file that looks like the list below.

invoice_id,customer_first_name,customer_last_name,part_number,quantity,total

18031,Hank,Scorpio,367,1,2.63
54886,Max,Power,171,3,50.79
19714,Jonathan,Frink,179,2,7.93
19714,Jonathan,Frink,378,2,32.34
22268,Gil,Gunderson,165,2,47.15
87681,Lionel,Hutz,218,1,50.83
84508,Lurleen,Lumpkin,257,1,81.95
34018,Lionel,Hutz,112,3,88.88
34018,Lionel,Hutz,386,3,86.04
34018,Lionel,Hutz,216,1,53.54
66648,Patty,Bouvier,203,3,70.47

I only want to print each line if its based off the criteria inputted by the user. For example, if the user inputs lname and then inputs Hutz the following would be printed out.

87681,Lionel,Hutz,218,1,50.83
34018,Lionel,Hutz,112,3,88.88
34018,Lionel,Hutz,386,3,86.04
34018,Lionel,Hutz,216,1,53.54
4 records found.

This is what I have so far...

salesdatafile= None
while True:
    salesdatafilename=input("Enter the name of the file:")
    try:
        salesdata= open(salesdatafilename, 'r')
        break
    except FileNotFoundError:
        print("{0} was not found".format ( salesdatafilename ))

search=input("Do you want to search by invoice id or lname? Please type id or lname: ")
idsearch=salesdata.readline() 

if search== 'id':
    idnumber=print(int(input('Please enter Id to search: ')))
    while idsearch != '':
        if idsearch== idnumber:
            print(idsearch)

else:
    lname=print(input('Please enter your last name: '))
    while idsearch != '':
        if idsearch== lname:
            print(idsearch)

All that is printing out is lname or id inputted by the user.

Python has a csv module built in that you should be utilizing. Check out the below example code:

import csv
salesdatafilename = r"path/to/file.txt"
data = csv.reader(open(salesdatafilename))
last_name_to_look_for = "Lionel"
for invoice_id, customer_first_name, customer_last_name, part_number, quantity, total in data:
     if customer_last_name == last_name_to_look_for:
        print(invoice_id, customer_first_name, customer_last_name, part_number, quantity, total)

Selecting rows in a CSV file based on column value, Assuming there are no embedded commas, awk is perfect for this. awk -F , '$4 == "abc" { print }' input.csv. I only want to print each line if its based off the criteria inputted by the user. For example, if the user inputs lname and then inputs Hutz the following would be printed out. 87681,Lionel,Hutz,218,1,50.83 34018,Lionel,Hutz,112,3,88.88 34018,Lionel,Hutz,386,3,86.04 34018,Lionel,Hutz,216,1,53.54 4 records found. This is what I have so far

In your code you are comparing the entire line to the idnumber or lname. Instead you might want to try something like

if lname in idsearch:
    <do something>

this will check if the lname is in the line somewhere.

python how to return a row from csv file based on user input?, You can use pandas to load your csv file. import pandas as pd df = pd.read_csv("​myfile.csv") user_input = "bob" for index, row in df.iterrows(): if  2 Print Lines from .csv based off criteria inputted from user Jun 3 '18 0 How to Count Line Items in SQL Aug 9 '18 0 Customizing Dates with CAST function using SQL Server Jul 29 '18

For performance, I will use a generator to read the lines so that my program doesn’t crash were this to be a bigger file. So I will yield each line in that file with a generator function. Since generators return iteratirs, I will then iterate and filter every line that does not contain my search value and return only those that contain my search value.

Filtering CSV files outside of a spreadsheet - Alicia Williams, I'll show you the solutions based on my specific data filtering needs for this case: a grab rows of a CSV that match certain criteria and create an output file for these rows. Use head to copy the header row from reviews.csv to a new output file, to tell grep what to search for and match on each input line. Re: Export data from Sheet as separate CSV files based on criteria FYI, Just did another test, and seems only file3.csv has that issue, the other ones seem to be ok, even if i add more data in the file, i was able to get correct files further than file3.csv

How do delete a specific row from a csv file based on a user input?, Your deletion logic needs changing. Try this: Hide Copy Code. for row in reader: if deletion != row['id']: writer.writerow(row) ; # write all  How can i export the values of the text box in html to a csv or a excel .I have created an HTML Form which will be used from a local computer and want the form data to be saved in CSV file. Each time the form is submitted, it should add a line in the CSV file. This needs to be run locally so cannot use PHP or JSP.

Chapter 14 – Working with CSV Files and JSON Data, Each line in a CSV file represents a row in the spreadsheet, and commas separate the The print() function call prints the number of the current row and the contents of the row. Read data from a CSV file as input for your Python programs. Extract all rows from a range that meet criteria in one column [Excel defined Table] The image above shows a dataset converted to an Excel defined Table, a number filter has been applied to the third column in the table. Here are the instructions to create an Excel Table and filter values in column 3. Select a cell in the dataset. Press CTRL + T

PowerTip: Get Row from CSV File Based On Value, PowerTip: Get Row from CSV File Based On Value. Doctor Scripto. Dr Scripto. February 19th, 2015. Summary: Use Windows PowerShell to get a row from a  Written by co-founder Kasper Langmann, Microsoft Office Specialist. Microsoft recently released 2 new functions, ‘MAXIFS’ and ‘MINIFS’, that will forever change the way you find minimum and maximum values in a range of cells (based on specific criteria). If we ever needed to find a maximum or minimum value in a range of cells based on

Comments
  • looking for guidance on the task. This is commendable, but SO may not be the best place for general guidance. Do you have a specific question? What I mean: is there a specific line or part of your code which is not behaving the way you expect?