Python Forum
How to print the 2nd column of a searched string in a csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print the 2nd column of a searched string in a csv
#1
Hi there can someone help

i am trying to search for a file name in a csv (in Column A) then when it finds it print just the 2nd column (Colum B) not the whole row, this prints both the coloumns in the csv
-------------------

import os

f_name = os.listdir(r'C:\Users\Peter\Documents\Python test\Files')[0]

print(f_name)

import csv

data=[]
with open ("test.csv") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
data.append(row)

col = [x[0] for x in data]

if f_name in col:
for x in range(0,len(data)):
if f_name ==data[x][0]:
action = print(data[x])

else:
print("File not listed")
Reply
#2
Assuming that a file named 'two_columns.txt' contains space separated two columns:

Output:
dummy.txt 7 secret.txt 42 public.txt 123
Then one way of getting desired result could be:

look_for = ('secret.txt', 'public.txt')

with open('two_columns.txt', 'r') as f:
    for row in f:
        first, second = row.split()
        if first in look_for:
            print(second)

# will print:
42
123
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks i will have a play, not sure tho
i have of pictures and a csv the picture file name in column A and location it was taken in Column B
i can get the code to look at the file name then go to the csv and search for the file name and then return the row of data, but i want it to just return the location column of that row. if u get what i mean
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove a space between a string and variable in print sie 5 1,759 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,929 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print first day of the week as string in date format MyerzzD 2 2,009 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  Creating new column with a input string drunkenneo 2 2,234 Apr-14-2021, 08:10 AM
Last Post: drunkenneo
  if a string has a digit - print tester_V 2 2,120 Jan-16-2021, 04:48 AM
Last Post: tester_V
  How to print string multiple times on new line ace19887 7 5,692 Sep-30-2020, 02:53 PM
Last Post: buran
  Print a certain string only the first time it appears in a test file buttercup 5 2,755 Jul-23-2020, 01:30 PM
Last Post: palladium
  How to print string multiple times separated by delimiter Mekala 1 1,887 May-23-2020, 09:21 AM
Last Post: Yoriz
  Print string after decode martinzeifang 1 1,829 Jan-02-2020, 10:16 AM
Last Post: buran
  Print string in a single line RavCOder 8 4,156 Nov-08-2019, 09:45 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020