473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[2.5] Reading a two-column file into an array?

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.
Jul 31 '07 #1
6 3934
Gilles Ganault wrote:
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?
The csv module? Or just .rstrip and .split?

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
I get my kicks above the wasteline, sunshine
-- The American, _Chess_
Jul 31 '07 #2
On Jul 31, 9:03 am, Gilles Ganault <nos...@nospam.comwrote:
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.
Jul 31 '07 #3
Nagarajan wrote:
On Jul 31, 9:03 am, Gilles Ganault <nos...@nospam.comwrote:
>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?
[...snip]
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.
I am similarly confused as to the nature of the original request, but for completeness' sake, I went by the same assumption of building a list of lists, and came up with this (which does not use the csv module). Nagarajan's code is more concise and just as readable IMO, but here's my take anyway:

a = []
b = []
handle = open(filename, 'r')

for line in handle.xreadlines():
col1,col2 = line.split('\t')
a.append(col1)
b.append(col2)

columns = [a, b]

-Jay
Jul 31 '07 #4
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
Jul 31 '07 #5
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
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()
That's what 2.5's with statement is all about...:

from __future__ import with_statement

def main():
with open('filename', 'rb') as f:
return list(csv.reader(f, delimiter='\t'))
Alex
Jul 31 '07 #6
On Tue, 31 Jul 2007 08:41:45 -0700, al***@mac.com (Alex Martelli)
wrote:
>That's what 2.5's with statement is all about...:
Thanks everyone. Python power :-)

from __future__ import with_statement
import csv

with open('import.csv', 'rb') as f:
for item in list(csv.reader(f, delimiter='\t')):
print item[0] + "," + item[1]
Aug 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Webster | last post by:
Hello, I have a program that asynchronously reads data from a host. However, whenever I call the BeginRead function, the async reading "loop" never seems to terminate. Why doesn't the EndRead...
1
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two...
3
by: fuenfzig | last post by:
Hi all, I want to use a single std::stringbuf for writing (by a std::ostream) and for reading (by a std::istream), concurrently in two threads. This came to my mind, because the code for reading...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
70
by: hstagni | last post by:
When i read a key using getchar() inside a loop, the program stops and wait for a key to be pressed. I actually want the program to continue its execution until a key is pressed. Look at this...
9
by: Bill Woessner | last post by:
Suppose I have a structure, foo, which is a POD. I would like to read and write it to disk as follows: std::ofstream outs; foo bar; outs.write(reinterpret_cast<char*>(&bar), sizeof(foo));...
4
by: Shark | last post by:
Hi, I need a help. My application reads data from COM port, this data is then parsed and displyed on: 1. two plotters 2. text box. I'm using Invoke method to update UI when new data is...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
6
by: rahul | last post by:
I am reading a binary packet : 32, 8, 8, 2, 1, 1, 4, 128 I am using the following structure to parse the data: struct header { unsigned int a:32; unsigned int b:8; unsigned int c:8; unsigned...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.