473,386 Members | 1,693 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,386 software developers and data experts.

csv.reader problem tab-delimiter - newbie

Hi all,

I use python 2.5 and want to read in a simple 5-columned, tab-
delimited ascii file "dummy.txt" such as as

1 2 3.0 4 5.6
4 6 77 8.2 19

I do

import csv
asciireader = csv.reader(open('dummy.txt'), delimiter='\t')

array1 = []
for entry in asciireader:
array1.append( entry )

however when I then

print array1

then the array does only have two elements [1 2 3.0 4 5.6]
and [4 6 77 8.2 19]. The tab-delimiter seems to have been
totally ignored! This is bad because I want to do some maths with the
numbers from the file later (after converting them into floats).

Can someone help?

Best regards,

Bernhard

Oct 21 '08 #1
4 5233
On Oct 21, 12:26*pm, hidd...@uni-duesseldorf.de wrote:
Hi all,

I use python 2.5 and want to read in a simple 5-columned, tab-
delimited ascii file "dummy.txt" such as as

1 *2 * *3.0 * *4 * * 5.6
4 *6 * *77 * * 8.2 *19

I do

import csv
asciireader = csv.reader(open('dummy.txt'), delimiter='\t')

array1 = []
for entry in asciireader:
*array1.append( entry )

however when I then

print array1

then the array does only have two elements [1 *2 * *3.0 * *4 * * 5.6]
and [4 *6 * *77 * * 8.2 *19]. The tab-delimiter seems to havebeen
totally ignored! This is bad because I want to do some maths with the
numbers from the file later (after converting them into floats).

Can someone help?

Best regards,

Bernhard
Most likely the file is not tab delimited. If you used an editor to
produce dummy.txt, check whether it uses soft tabs and disable it
temporarily.

HTH,
George
Oct 21 '08 #2
>
Most likely the file is not tab delimited. If you used an editor to
produce dummy.txt, check whether it uses soft tabs and disable it
temporarily.

HTH,
George
Most likely the file is not tab delimited. If you used an editor to
produce dummy.txt, check whether it uses soft tabs and disable it
temporarily.

HTH,
George
grrr thanks George, thanks Daniel, that's it. Works fine with real
tabs. Now this brings me to a follow-up: I have not prepared the input
ascii file, I just got if for post-processing (23 MB). Now it seems
that I would have to replace all series of spaces in this file with
real tabs at first.. How would you do that? Are there text editors
capable of doing this?

Best regards,

Bernhard
Oct 21 '08 #3
2008/10/21 <hi*****@uni-duesseldorf.de>:
grrr thanks George, thanks Daniel, that's it. Works fine with real
tabs. Now this brings me to a follow-up: I have not prepared the input
ascii file, I just got if for post-processing (23 MB). Now it seems
that I would have to replace all series of spaces in this file with
real tabs at first.. How would you do that? Are there text editors
capable of doing this?
If the file you got isn't really tab delimited, you shouldn't convert
it to a tab delimited file before processing.

Just process it as it is, a multiple-space-delimited file.

f = open('file.txt')
for line in f:
line = line.strip().split()
# do whatever with data
print line
f.close()

Clóvis
Oct 21 '08 #4
On Oct 21, 1:44*pm, hidd...@uni-duesseldorf.de wrote:
Most likely the file is not tab delimited. If you used an editor to
produce dummy.txt, check whether it uses soft tabs and disable it
temporarily.
HTH,
George
Most likely the file is not tab delimited. If you used an editor to
produce dummy.txt, check whether it uses soft tabs and disable it
temporarily.
HTH,
George

grrr thanks George, thanks Daniel, that's it. Works fine with real
tabs. Now this brings me to a follow-up: I have not prepared the input
ascii file, I just got if for post-processing (23 MB). Now it seems
that I would have to replace all series of spaces in this file with
real tabs at first.. How would you do that? Are there text editors
capable of doing this?
If your file consists of numbers only, you don't even need csv; you
can read the whole file and convert it to floats in a single readable
line:

data = [map(float, line.split()) for line in open('dummy.txt')]

HTH,
George
Oct 21 '08 #5

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

Similar topics

2
by: Greg | last post by:
I have some html code that is displayed on a wxHTMLwindow in a Python application. One of the ahref links is to a pdf document. When I click the link, I get what appears to be some sort of header...
4
by: Bruce Wolfe | last post by:
We are developing an application using C#.net and we need to provide support for screen readers. Jaws works pretty good except that it can't read group box labels on dialog forms. The same version...
8
by: Stephan | last post by:
I'm fairly new to python and am working on parsing some delimited text files. I noticed that there's a nice CSV reading/writing module included in the libraries. My data files however, are odd...
8
by: Todd Bright | last post by:
Is there a way to get the current XmlNode from the reader while in the validation event handler? What I'd like to do is display the error message along with the name of its parent node. In my...
4
by: Lumpierbritches | last post by:
Thank you for all your help and assistance, it's greatly appreciated. Is there a way in Access, any version to capture the mag stripe information and place in appropriate textboxes? i.e. Lastname,...
1
by: Madhusudhan.Penta | last post by:
Hi All, Can anybody provide me with a smart CSV , Tab Delimited Reader Thanx in Advance
18
Dököll
by: Dököll | last post by:
Greetings and Salutations! You deserve a look at the finished product you have helped build. Will only post part of the code, judging by the massive if statement included: 'Application:...
1
by: vbDavidC | last post by:
Hi, I am fairly new to .net and objects. I learned to create a reader object in method 1, however if I wanted to create multiple select queries in the same module I did not know how to reuse...
0
by: bill | last post by:
Can someone please show me what to do with this to show the data in a dataGrid for example? I'm not really sure what to do with it. I'm used to Vb6. Dim Con = New...
0
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi all people, everybody, We have multiple versions of Acrobat Reader from 5.x to 8.x, I want to create a method in C# or VB.NET to check to see if the registry key for the versions...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.