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


Messages In This Thread
How to print the 2nd column of a searched string in a csv - by ptey07 - Jan-11-2020, 10:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove a space between a string and variable in print sie 5 1,840 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,991 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print first day of the week as string in date format MyerzzD 2 2,057 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  Creating new column with a input string drunkenneo 2 2,283 Apr-14-2021, 08:10 AM
Last Post: drunkenneo
  if a string has a digit - print tester_V 2 2,186 Jan-16-2021, 04:48 AM
Last Post: tester_V
  How to print string multiple times on new line ace19887 7 5,817 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,825 Jul-23-2020, 01:30 PM
Last Post: palladium
  How to print string multiple times separated by delimiter Mekala 1 1,931 May-23-2020, 09:21 AM
Last Post: Yoriz
  Print string after decode martinzeifang 1 1,856 Jan-02-2020, 10:16 AM
Last Post: buran
  Print string in a single line RavCOder 8 4,278 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