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



On Jul 31, 9:03 am, Gilles Ganault <nos...@xxxxxxxxxx> wrote:
Hello

I'm sure there's a much easier way to read a two-column, CSV file into
an array, but I haven't found it in Google.

Should I use the Array module instead?

=========
a = []
i = 0

#item<TAB>item<CRLF>
p = re.compile("^(.+)\t(.+)$")

for line in textlines:
m = p.search(line)
if m:
a[i,0] = m.group(1)
a[i,1] = m.group(2)
i = i + 1

for i in a.count:
for j in 2:
print a[i,j]
=======

Thank you.

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

----------------------------
I don't think you can have multidimensional arrays.
Did you test you program? It did not work for me.
I think mine would suit your requirements as the output is a list of
lists.


.



Relevant Pages

  • Re: Trying to build a page that will read a csv file...
    ... > I can't figure out how to get the results to display in a multi-column ... >> Use fileto load the csv file into an array. ... >> Use a loop to iterate through every element in the array, ...
    (php.general)
  • Re: Trying to build a page that will read a csv file...
    ... cough cough ) I decided it was ... I've even managed to get php to display the database ... > Use fileto load the csv file into an array. ... > Use a loop to iterate through every element in the array, ...
    (php.general)
  • Re: Load a string of CSV from memory into a dataset
    ... you should avoid writing to the disk as ... - which after decryption and just before writing to the CSV ... Then run a foreach on the resulting array and split each line (array ... the csv file is being loaded into a datatable, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Read an Input File
    ... I'm using the code below to read a CSV file into an Array ... Line Input #1, recorddata ... But the problem is that one of the Cells in one row of my csv file contains a Carriage return character so when it comes to that cell it thinks that is the end of line and it breaks my one line to 2 lines. ...
    (microsoft.public.vb.general.discussion)
  • Re: CSV to access in ASP... making my head spin
    ... user would like to be able to upload a CSV file, ... putting the CSV in an array to save the information ... Please reply to the newsgroup. ...
    (microsoft.public.scripting.vbscript)