-1

Goal:

My goal is to get words out of second column from CSV. I am having a difficulty searching for what I need, I am also not familiar with data when using Python.

Reason I am doing this is, because of the following:

We have a database ran by an external company. Within one of the tables, one of the columns should only be barcodes.

Here is the problem:

I have a .csv file with this data:

columnOne,columnTwo
"YOBA001OL","501",
"YOBA001OL","Yo Bakehouse",
"WILD004OL","Wild",
"TWOB009OL","7897",
"S079R36D05","0007",

How can I extract only values that is a "word" in the 2nd column and not numbers?

Here is my desired output:

columnOne,columnTwo
"YOBA001OL","Yo Bakehouse",
"WILD004OL","Wild",

Here is what I have at the moment:

import csv


with open('barcodes.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        print(row)
Eduards
  • 1,734
  • 2
  • 12
  • 37

2 Answers2

1

Instead of printing, check if it is an int

output = []
for row in csv_reader:
    try:
        int(row[1]) # second column
    except:
        output.append(row)

print(', '.join(output))
politinsa
  • 3,480
  • 1
  • 11
  • 36
  • I think your first answer was better. Thats the one I have used but amended this: `except: print(row[0] +', '+ row[1])` – Eduards Jan 17 '20 at 16:06
  • I edited so you have the desired output. I've saved into a variable in case you'd want to write it back to a CSV, it would be easier – politinsa Jan 17 '20 at 16:09
1

You can try like this:

with open('barcodes.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        if not row[1].isdigit():
            print(row)
zipa
  • 27,316
  • 6
  • 40
  • 58