csv.reader problem tab-delimiter - newbie

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hidding@uni-duesseldorf.de

    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

  • George Sakkis

    #2
    Re: csv.reader problem tab-delimiter - newbie

    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

    Comment

    • hidding@uni-duesseldorf.de

      #3
      Re: csv.reader problem tab-delimiter - newbie

      >
      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

      Comment

      • Clovis Fabricio

        #4
        Re: csv.reader problem tab-delimiter - newbie

        2008/10/21 <hidding@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().sp lit()
        # do whatever with data
        print line
        f.close()

        Clóvis

        Comment

        • George Sakkis

          #5
          Re: csv.reader problem tab-delimiter - newbie

          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

          Comment

          Working...