472,981 Members | 1,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 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 3918
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.