0

I am new to python and was trying to read a specific column from a CSV file.I am editing the problem now.

Here is the CSV file for eg:

one two three four
1    3    5    7
2    3    5    7

I wrote a very simple Python program as :(thanks to Amrit below for the modified mode).

import csv
with open('Activity_PSR.csv','rU') as csvFile:
    reader=csv.reader(csvFile,delimiter=',')
    for row in reader:
         print row[6]
csvFile.close()

I am getting the specific column with Column header.I want the output of just the elements without the column header.

Once I get the output of this column ,I want to store it in a variable and use it in a CURL command to POST Data. CURL command equiv I beleive is urlib.

Here is the CURL POST Command:

curl -v -X POST -d ‘{“Organization_Id___ORACO__Account_Order":300100051155060,"}' slcaf961.us.oracle.com:10615/crmCommonApi/resources/11.1.10/__ORACO__Order_c
 -H "Content-Type:application/vnd.oracle.adf.resourceitem+json" --user “sales_representative”

And the urllib code I am planning to use:

import urllib2
data = '{“Organization_Id___ORACO__Account_Order":300100051155060"}'
url = 'slcad745.us.oracle.com:10618/crmCommonApi/resources/11.1.10/__ORACO__Order_c'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
for x in f:
    print(x)
f.close()

In the data field here Instead of passing 300100051155060 directly I want to use the value I got from the CSV above and pass that as the Account-Id.

How to pass the value I got from the CSV and pass it to the JSON payload as a parameter ? Need to store the value I got from CSV and store it in a Variable and pass it to the JSON payload/data as a parameter and execute the POST method. Request folks to help me on how to acheive the above scenario

Jai
  • 295
  • 2
  • 5
  • 11

3 Answers3

0

This means that a row does not have a column on index 2. You need to create a guard:

for row in csv_f:
    # make sure row has value on column 2 - otherwise skip row
    if len(row) > 2:
        print row[2]

You can also use an exception, which in Python is considered cheap:

for row in csv_f:
    try:
        print row[2]
    except IndexError:
        print("skipping row %s" % str(row))

If you know all rows need to have a value there, make sure you're using the right delimiter using the delimiter argument to the reader's constructor.

Also, make sure you're closing the file after the loop:

for row in csv_f:
    # do stuff with 'row'
csv_f.close()
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • Hi Reut the problem its not selecting particular column values.In the CSV I have 4 columns and values in all 4 columns I am not sure why its still giving me list index out of range. – Jai Jul 31 '15 at 20:18
  • All I want to do is get say column 2 values as output and store it in a variable for further use.How to do that in this scenario. – Jai Jul 31 '15 at 20:18
  • Can you show the actual input? If you really have 4 values, this will not happen. – Reut Sharabani Jul 31 '15 at 20:22
  • Sure I have Input CSV uploaded here I used – Jai Jul 31 '15 at 20:31
  • What if you tried specifying a delimiter and removing the dialect? `csv_f = csv.reader(open('test.csv','rU'), delimiter=',')` – Reut Sharabani Jul 31 '15 at 20:43
  • Hi Reut thanks for the suggestion making tge change worked I have updated my question with the actual scenario I an looking for can you please check and suggest thanks for the help – Jai Aug 02 '15 at 07:16
  • This is a followup that completely changes the relevance of this answer as well as other. I suggest you accept an answer with respect to your original question and post a separate new question. – Reut Sharabani Aug 02 '15 at 07:18
  • Sure Reut,I have accepted the answer and opened new topic with my q: – Jai Aug 02 '15 at 18:32
  • http://stackoverflow.com/questions/31775208/reading-sepcific-csv-column-value-and-passing-it-to-curl-urllib-as-param-in-pyth – Jai Aug 02 '15 at 18:32
0

You should consider working with pandas. It has a function called read_csv that works very well. In your case the code to print out column three is

from pandas import *
csv_f=read_csv('test.csv', sep=r"\s*")
for a in csv_f['three']:
     print a

Of course, it has a bunch of functionalities that you should check out and see if you can make an use of.

TomCho
  • 3,204
  • 6
  • 32
  • 83
  • Doesn't really help...some example code would be nice to add :) – heinst Jul 31 '15 at 20:03
  • @heinst yeah I was in the process of writing it when you commented! Please reconsider you -1? :) – TomCho Jul 31 '15 at 20:05
  • Hi Tom if you can give me a example code on how to do my scenario using Pandas it would be very helpful.Thanks for the help – Jai Jul 31 '15 at 20:19
  • 1
    pandas is overkill for OP's request. OP's inability to understand things like lengths of lists is just going to cause another bug someday whether using pandas or pure Python. A more helpful answer would explain what OP is doing wrong. – dg99 Jul 31 '15 at 21:19
  • @Jai I modified the answer to do what I think you want, which is to print column 'three'. – TomCho Jul 31 '15 at 21:47
  • Thanks Tom will try it and let you know – Jai Jul 31 '15 at 23:11
0

Use this code instead:

import csv
with open('test.csv','r') as csvFile:
    reader=csv.reader(csvFile,delimiter=',')
    reader.next()   # if you don't want to print columns headers
    for row in reader:
         print row[2]
csvFile.close()
Amritbir Singh Gill
  • 366
  • 1
  • 3
  • 12
  • Thanks Amritbir will try it out – Jai Jul 31 '15 at 23:12
  • Hi Amritbir how can I get the column values without the header.At the moment it prints the column with the Column header.how to ignore the Column header and print get the column values alone. – Jai Aug 01 '15 at 23:36