Skip the first column when reading a csv file Python

abn

I'm trying to read a csv file and extract required data from it. My code looks like below.

import csv
file = "sample.csv"
def get_values_flexibly(file, keyword):
    def process(func):
        return set([func(cell)] + [func(row[index]) for row in reader])

    with open(file, 'r') as f:
        reader = csv.reader(f)
        first_row = reader.next()
        if keyword in first_row:
            return str(list(set([row[first_row.index(keyword)] for row in reader])))
        for index, cell in enumerate(reader.next()):
            if cell.endswith(' ' + keyword):
                return str(list(set(process(lambda cell: cell[:-len(keyword) - 1]))))
            elif cell.split(':')[0].strip() == keyword:
                return str(list(set(process(lambda cell: cell.split(':')[1].strip()))))
print get_values_flexibly(file, 'data')

where sample.csv looks something like below

sample.csv

h1,h2,h3
a data,data: abc,tr
b data,vf data, gh
k data,grt data, ph

I'd like to exclude first column from the output. My current output is ['a','k','b'] but I'd like it to be ['abc', 'vf', 'grt'] instead. How can I achieve this using csv reader?

EDIT- I have multiple files. Each file could have different headers and number of columns varies too. I'd like to have a script that works for all the files. Also, the header of the first column is always the same, "sample_column" for instance. I'd like to skip data from column with header "sample_column".

BorrajaX

Ok, so removing the data (or whichever the keyword is) could be done with a regular expression (which is not really the scope of the question but meh...)

About the regular expression:

Let's imagine your keyword is data, right? You can use this: (?:data)*\W*(?P<juicy_data>\w+)\W*(?:data)* If your keyword was something else, you can just change the two data strings in that regular expression to whatever other value the keyword contains...

You can test regular expressions online in www.pythonregex.com or www.debuggex.com

The regular expression is basically saying: Look for zero or more data strings but (if you find any) don't do anything with them. Don't add them to the list of matched groups, don't show them... nothing, just match them but discard it. After that, look for zero or more non-word characters (anything that is not a letter or a number... just in case there's a data: or a space after , or a data--> ... that \W removes all the non-alphanumerical characters that came after data ) Then you get to your juicy_data That is one or more characters that can be found in "regular" words (any alphanumeric character). Then, just in case there's a data behind it, do the same that it was done with the first data group. Just match it and remove it.

Now, to remove the first column: You can use the fact that a csv.reader is itself an iterator. When you iterate over it (as the code below does), it gives you a list containing all the columns found in one row. The fact that it gives you a list of all the rows is very useful for your case: You just have to collect the first item of said row, since that's the column you care about (you don't need row[0], nor row[1:])

So here it goes:

import csv
import re

def get_values_flexibly(csv_path, keyword):
    def process(func):
        return set([func(cell)] + [func(row[index]) for row in reader])
    # Start fo real!
    kwd_remover = re.compile(
        r'(?:{kw})*\W*(?P<juicy_data>\w+)\W*(?:{kw})*'.format(kw=keyword)
    )
    result = []
    with open(csv_path, 'r') as f:
        reader = csv.reader(f)
        first_row = [kwd_remover.findall(cell)[0] for cell in reader.next()]
        print "Cleaned first_row: %s" % first_row
        for index, row in enumerate(reader):
            print "Before cleaning: %s" % row
            cleaned_row = [kwd_remover.findall(cell)[0] for cell in row]
            result.append(cleaned_row[1])
            print "After cleaning: %s" % cleaned_row
    return result

print "Result: %s" %  get_values_flexibly("sample.csv", 'data')

Outputs:

Cleaned first_row: ['h1', 'h2', 'h3']
Before cleaning: ['a data', 'data: abc', 'tr']
After cleaning: ['a', 'abc', 'tr']
Before cleaning: ['b data', 'vf data', ' gh']
After cleaning: ['b', 'vf', 'gh']
Before cleaning: ['k data', 'grt data', ' ph']
After cleaning: ['k', 'grt', 'ph']
Result: ['abc', 'vf', 'grt']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to skip the first row when reading a csv file?

python pandas not reading first column from csv file

How to skip blank cells when reading a csv file in Java?

delete first column when exported excel or csv file in python

Skip first couple of lines while reading lines in Python file

Skip first couple of lines while reading lines in Python file

CSV.foreach Not Reading First Column in CSV File

skip second row of dataframe while reading csv file in python

Reading in column blocks from CSV file PYTHON

Skip first row when overwriting the CSV file in PHP

Elegant way to process and skip first line(s) (containing metadata) when reading an array from file in Python (possibly pandas)?

How to skip lines when reading a yaml file in python?

Reading first element of each column and then the entire row in csv file

only reading first N rows of csv file with csv reader in python

UnicodeDecodeError when reading CSV file in Pandas with Python

encoding issue when reading CSV file with python

In file reading, how to skip the Nth first lines

Reading Text file in C, skip first line

How to skip reading the first line of file?

Creating a new column when reading the file in python

Delete the first column from a csv file in Python

skip double quotes when reading csv file using apache commons csv

Skip Header Row when reading in a CSV

Skip lines when reading from csv in pandas

How to skip second line is csv file while maintaining first line as column names with read_csv?

Python add entire column to csv file, without reading the file content

Skip the headers when editing a csv file using Python

Python: for loop stops at first row when reading from csv

c++ Skip first line of csv file

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive