Hack Forums
[Python] CSV COLUMN READER - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] CSV COLUMN READER (/showthread.php?tid=5568631)



CSV COLUMN READER - thePoltergeist - Mar 7, 2017

I have a CSV file that looks like this:

htt ps://gyazo. c om/51b69c4d128286521e86cb5715b5fd9a

I need a program that does this:

If Title = 'Title 1(2, 3, 4)' Data should = '1'("Live", 3.6)

The program basically needs to check that each column holds the correct data type.

I have tried to do this loads of way but everything seems to work with rows not columns and it is confusing me.

Thanks a lot guys


* - (╯°□°)╯︵ ┻━━┻ - Mar 7, 2017

Stick to one thread please, you've created at least three.


* - thePoltergeist - Mar 7, 2017

(Mar 7, 2017 07:43 AM)(╯°□°)╯︵ ┻━━┻ Wrote:  Stick to one thread please, you've created at least three.

Yeah sorry I am stressing out. This needs to be done.


* - (╯°□°)╯︵ ┻━━┻ - Mar 7, 2017

Is there any reason why you can't use Excel to do this?


* - thePoltergeist - Mar 7, 2017

It's not quick enough and can only be used on machines with excel


* - Malicious Intent - Mar 7, 2017

Code
import sys, csv

with open('file.csv', 'rb') as csvfile:

    # get number of columns
    for line in csvfile.readlines():
        array = line.split(' ')
        first_item = array[0]

    num_columns = len(array)
    csvfile.seek(0)

    reader = csv.reader(csvfile)
    included_cols = [1,2,3,4]

    for row in reader:
             content=list(row[i] for i in included_cols)
             if 'blah blah' not in content:
                newdata=content
                print newdata

Here is an example code for csv .. see the row part? just change it to whatever you need it to do ..


* - thePoltergeist - Mar 7, 2017

(Mar 7, 2017 10:01 AM)Malicious Intent Wrote:  
Code
import sys, csv

with open('file.csv', 'rb') as csvfile:

    # get number of columns
    for line in csvfile.readlines():
        array = line.split(' ')
        first_item = array[0]

    num_columns = len(array)
    csvfile.seek(0)

    reader = csv.reader(csvfile)
    included_cols = [1,2,3,4]

    for row in reader:
             content=list(row[i] for i in included_cols)
             if 'blah blah' not in content:
                newdata=content
                print newdata

Here is an example code for csv .. see the row part? just change it to whatever you need it to do ..

Very helpful comment thank you