-4

i'm fairly new to python and am looking for some help. What i would like to do is read a csv file and then use a for loop with an if statement to locate at rows of that data contain a value and print it out with a header and some formatting using f'.

The issue i seem to have it when finding the data using the if statement, im unsure what i can output the data to, which will then enable it to be printed out (the search output could contain multiple rows and columns):

with open(r'data.csv', 'r') as csv_file:
    # loop through the csv file using for loop
    for row in csv_file:
        # search each row of data for the input from the user
        if panel_number in row:
            ??
Barmar
  • 741,623
  • 53
  • 500
  • 612
MattG
  • 19
  • 6
  • so do `print(f"

    {row}

    ")` and you will print the content of row as a H1 header
    –  Sep 08 '22 at 15:48
  • `??` could be: `print(row)`? You say you want the header, which is generally the first row of the file, so you likely want to save that to a variable so you can print it out when you print the row out. – JNevill Sep 08 '22 at 15:48
  • Thanks for that - let me clarify further. So there is multiple rows of data which may match in the 'if statement', and i would like to display these in the console window. is it possible to write these to a list, then print out the list at the end? The data will contain multiple columns and rows. – MattG Sep 08 '22 at 15:51
  • shouldnt you read the file content first ?? : https://www.pythontutorial.net/python-basics/python-read-text-file/, https://docs.python.org/3/library/csv.html – pippo1980 Sep 08 '22 at 15:52
  • 1
    @pippo1980 `for row in csv_file:` reads the content line by line. – Barmar Sep 08 '22 at 15:56
  • @MattG You can do `matched_lines = []` before the loop, `matched_lines.append(row)` in the `if` statement, and `print(matched_lines)` at the end. – Barmar Sep 08 '22 at 15:57
  • shouldnt you read the file content first ?? wirh a csv reader tool/parser https://docs.python.org/3/library/csv.html ?? – pippo1980 Sep 08 '22 at 16:00
  • ok got it panel_number is the substring to look for in each line – pippo1980 Sep 08 '22 at 16:03
  • So a follow up question, what about if i just want to search the first column in the CSV? would it be `if panel_number in row[0]:` – MattG Sep 08 '22 at 16:11
  • Use the `csv` module to parse the file into fields. See the link in pippo's comment above. – Barmar Sep 08 '22 at 16:14
  • Sorry @barmar, i dont follow? should i have before the loop `x.readlines` – MattG Sep 08 '22 at 16:17
  • for row in csv_file: reads the content line by line. – pippo1980 Sep 08 '22 at 17:55

1 Answers1

0

Use the csv module. Then in your if statement you can append the row to a list of matches

import csv

matched_rows = []
with open(r'data.csv', 'r') as file:
    file.readline() # skip over header line -- remove this if there's no header
    csv_file = csv.reader(file)
    for row in csv_file:
        # search each row of data for the input from the user
        if row[0] == panel_number:
            matched_rows.append(row)

print(matched_rows)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • ` matched_lines = [] # open the csv file with open(r'data.csv', 'r') as file: # loop through the csv file using for loop file.readline() csv_file = csv.reader(file) for row in csv_file: # search each row of data for the input from the user if row[0] == panel_number: matched_lines.append(row) print(matched_lines)` but i get IndexError: list index out of range – MattG Sep 08 '22 at 16:36
  • 1
    You must have blank lines in the file. Add an `if` statement that checks `if len(row) == 0:` and skips that row. – Barmar Sep 08 '22 at 16:40
  • https://stackoverflow.com/questions/4842057/easiest-way-to-ignore-blank-lines-when-reading-a-file-in-python – pippo1980 Sep 08 '22 at 18:04