3

I learned how to script in python with version 2.7; however, the system I am coding on now only has version 2.4.3. I am trying to open a file titled "input.csv", read columns 0, 1, 2 and 3, while simultaneously skipping the first row since it contains header information that I do not want. The code I am attaching works fine with Python 2.7.9, but does not work with 2.4.3. Can someone point me in the right direction as to how this block of code should be written.

import csv          # imports the library that enables functionality to read .csv files

MATPRO_Temperature  = []  # List to hold MATPRO experimental temperatures
MATPRO_Density      = []  # List to hold MATPRO experimental densities
MATPRO_Conductivity = []  # List to hold MATPRO experimental thermal conductivities
MATPRO_References   = []  # List to hold MATPRO references for each measurement

File_Name = 'Input.csv'   # - The relative address for the MATPRO database containing
                          #   the thermal conductivity measurements

# This section opens the .csv file at the address 'File_Name' and reads in its data into lists
with open(File_Name) as csvfile:
  next(csvfile)  # This forces the reader to skip the header row of hte .csv file
  readCSV = csv.reader(csvfile, delimiter = ',')
  for row in readCSV:
    MATPRO_Temperature.append(row[0])
    MATPRO_Density.append(row[1])
    MATPRO_Conductivity.append(row[2])
    MATPRO_References.append(row[3])
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Jon
  • 1,621
  • 5
  • 23
  • 46
  • Python 2.4 is over 10 years old and has been EOL for years. Is there any reason why you're stuck on 2.4? Can't you upgrade to 2.7? – Bjorn Aug 05 '15 at 17:29
  • Bjorn, unfortunately in the business I work in, things seldom ever make sense. Python is freeware, so upgrading should be simple, but our IT group refuses to update the Python code on our Linux system and I am not allowed to do it myself. – Jon Aug 05 '15 at 17:43

3 Answers3

4

According to https://docs.python.org/release/2.4.3/lib/csv-contents.html, you need to read the csv file before calling next on it. Also, the with keyword isn't in Python till version 2.5.

 csvfile = open(File_Name, 'r') # 'r' -> read only
 try:
      readCSV = csv.reader(csvfile, delimiter = ',')
      next(readCSV) # move it here, and call next on the reader object
      for row in readCSV:
            ...
 finally:
       csvfile.close()


Explanation: The reason for try and finally is explained here How to safely open/close files in python 2.4, but basically, you want to make sure you close the file properly (which is what the with keyword does) even if there is an error.
Community
  • 1
  • 1
dhuang
  • 909
  • 4
  • 18
  • Unfortunately Python 2.4.3 does not like the with open() command – Jon Aug 05 '15 at 17:49
  • There is no `next()` function in Python 2.4; use `reader.next()` instead. Also, use `rb` mode for csv file in Python 2. – jfs Aug 06 '15 at 19:13
1

Another way would be to use the enumerate() function

  f = open("Input.csv")
for i, line in enumerate(f):
    if i==0:
        continue

...
DaFreed
  • 11
  • 1
  • 4
1

You could use reader.next()

reader = csv.DictReader(reading_file,fieldnames=firstline,delimiter=',')

reader.next()
Tarun Ande
  • 31
  • 7