Re: Reading a two-column file into an array?



On Mon, 30 Jul 2007 21:57:17 -0700, Nagarajan wrote:

a = []
import csv
reader = csv.reader(open("filename", "r"), delimiter='\t' )
for row in reader:
a.append( row )

I would keep a reference to the file to close it properly and the loop can
be replaced by a call to `list()`:

import csv

def main():
data_file = open('filename', 'rb')
a = list(csv.reader(data_file, delimiter='\t'))
data_file.close()

Ciao,
Marc 'BlackJack' Rintsch
.



Relevant Pages

  • Re: Reading a two-column file into an array?
    ... import csv ... I would keep a reference to the file to close it properly and the loop can ... def main: ...
    (comp.lang.python)
  • Re: "groupby" is brilliant!
    ... I am using csv ... various break fields, comparing the values on each row with the saved ... reading about 'groupby'. ... for row in reader: ...
    (comp.lang.python)
  • Re: "groupby" is brilliant!
    ... I am using csv ... various break fields, comparing the values on each row with the saved ... reading about 'groupby'. ... for row in reader: ...
    (comp.lang.python)
  • Big problem with @array and Chomp ... I think :o
    ... I have a CSV content module. ... can take the contents of a CSV file, paste it into the textarea, and the ... Here is the code I use to add the entry ... Through a loop, I split each ...
    (comp.lang.perl.misc)
  • Re: Bulk User Import Using a VBScript
    ... Using VBScript, you can use ADO and the FileSystemObject to read in a CSV ... then use a big old loop with variables instead of explicitly defining ...
    (microsoft.public.windows.server.active_directory)