0

My goal is to search a single column for a particular value in a CSV file and print splitted_line[2] I have a CSV file that has 800+ rows. Column one has a list of numbers associated with each line. Shown below:

96,"Happy snappy chicken",-rhubarb,https://v.redd.it/kwarsqx9m1s51
97,"Party Located. Engaging",Mernerner,https://i.imgur.com/lNs61d9.jpg
98,"[TikTok] This conure likes to BONK",lilgothbabey,https://v.redd.it/l7mm4cakjtr51
99,"He escaped his outside cage and just made this little smug face",Molly_the_yorkie_poo,https://i.redd.it/naqfaoq801s51.jpg
100,"Little pie",Sweety_pie19,https://i.redd.it/s9i2xc06n0s51.jpg
101,"After a bad visit with the vet, Alex is back to his attention-seeking self!",jballen93017,https://i.redd.it/cfbyevqyozr51.jpg

My script below is supposed to search through that first column, and if the number == first column then print the entire row. Except when I do this, it prints all 800+ rows. Any help?

import pyautogui
from datetime import datetime, time
import os, sys, random
import io
import csv
from time import sleep

folder_path = "PATH"

def upload_photo():
    list = os.listdir(folder_path)
    number_files = len(list) # Returns the number of files in a directory as an integer

    try:
        for i in range(number_files):
            random_file = random.choice(os.listdir(folder_path))                 # Chooses random file in folder
            print(random_file)                                                   # Print file name
            nfile = int(''.join(filter(str.isdigit, random_file.split('.')[0]))) # Splits the extention
            print (nfile)                                                        # Prints the number associated with the image
            break
    except:
        print("Hit except")

    with open("output_reddit.csv", 'r') as csvfile:
        for line in csvfile:
            splitted_line = line.split(',')
            if splitted_line[0] == nfile:
                print(splitted_line[1])

upload_photo()
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • @martineau I tried the solution you presented me before I asked this question and it was not of use, which is why I'm asking again. Please open it back up –  Oct 13 '20 at 16:44
  • Then there must be a bug in your code — which is more difficult to understand than necessary because it's **not** a [mre] (for instance what are all those unneeded `import`s at the beginning?). Also, in the beginning, you say you want to "**print splitted_line[2]**" but the code in the last `for` loop has `print(splitted_line[1])`. – martineau Oct 13 '20 at 17:00

0 Answers0