Cesar
Cesar

Reputation: 617

Select specific columns from CSV file

My code is able to get the 28 columns of a text file and format/remove some data. How Can I select specific columns? The columns I want are 0 to 25, and column 28. What is the best approach?

Thanks in advance!

import csv
import os

my_file_name = os.path.abspath('NVG.txt')
cleaned_file = "cleanNVG.csv"
remove_words = ['INAC-EIM','-INAC','TO-INAC','TO_INAC','SHIP_TO-inac','SHIP_TOINAC']


with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
    writer = csv.writer(outfile)
    cr =  csv.reader(infile, delimiter='|')
    writer.writerow(next(cr)[:28])
    for line in (r[0:28] for r in cr):

        if not any(remove_word in element for element in line for remove_word in remove_words):
         line[11]= line[11][:5]

         writer.writerow(line)
infile.close()
outfile.close()

Upvotes: 0

Views: 491

Answers (2)

Haifeng Zhang
Haifeng Zhang

Reputation: 31885

exclude column 26 and column27 from row using filter():

for row in cr:
    content = list(filter(lambda x: row.index(x) not in [25,26], row))
    # work with the selected columns content

Upvotes: 1

Ohjeah
Ohjeah

Reputation: 1307

Have a look at pandas.

import pandas as pd

usecols = list(range(26)) + [28]
data = pd.read_csv(my_file_name, usecols=usecols)

You can also conveniently write the data back to a new file

with open(cleaned_file, 'w') as f:
    data.to_csv(f)

Upvotes: 3

Related Questions