473,324 Members | 2,246 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,324 software developers and data experts.

reading data columns into separate lists

PythonGurus,

I would like to read the ascii data from a file with many columns
into a list. I want the type to be float.

data=open('dat1.dat','r')

lines=data.readlines()
print lines

Here, the data x1,x2,x3,x4 are separated
by tabs and end with new line.
1\t2\t3\t4\n
....
....

Is there a way to read the delimiter separated data
straightaway to a list of fixed type.

Thanks in advance,
viswa.
Jul 18 '05 #1
1 2287
Viswa wrote:
I would like to read the ascii data from a file with many columns
into a list. I want the type to be float.

data=open('dat1.dat','r')

lines=data.readlines()
print lines

Here, the data x1,x2,x3,x4 are separated
by tabs and end with new line.
1\t2\t3\t4\n
...
...

Is there a way to read the delimiter separated data
straightaway to a list of fixed type.


from random import random

# generate sample data (10 lines, 5 columns)
f = file("tmp.txt", "w")
for n in range(10):
f.write("\t".join([str(random()) for n in range(5)]))
f.write("\n")
f.close()

# read it into a list of lists
rows = [map(float, line.split()) for line in file("tmp.txt")]

print len(rows), "rows read"
print "contenst of 4th row:"
print rows[3]

For more heterogeneous data you may have a look at the csv module (new in
Python 2.3).

Peter
Jul 18 '05 #2

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

Similar topics

2
by: Satish Chimakurthi | last post by:
Hi all, This is in continuation of my email today regarding reading data files. I am stuck with one more problem now. Here are my two data files: a. fluid_grid.dat 1.00000000000000 ...
35
by: Troll | last post by:
Hi, I need to write a script which reads some data and reports the findings. Just to give you an idea the structure is similar to the following. Data input example: HEADING 1 **********...
0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
5
by: JL | last post by:
Hi, I was asked the following question at work: "Assume that you have 5000 columns and 5000 rows of data. You take these data from the database and need to show them in a table. Before...
6
by: Steve | last post by:
I realize that this probably isn't a best practice, but I'm working with legacy code that has a query stored in one column of a table. Because the queries vary, the JSP page that selects a query...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: madhankrish | last post by:
hi all, can someone give me the C code to read individual columns of files into separate arrays? i have a .CSV file which has 6 columns and 2000 rows... i have to read each of the 6 columns into a...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.