tags:

views:

709

answers:

5

Hi,

I have some text file like this, with several 5000 lines:

5.6  4.5  6.8  "6.5" (new line)
5.4  8.3  1.2  "9.3" (new line)

so the last term is a number between double quotes.

What I want to do is, using Python (if possible), to assign the four columns to double variables. But the main problem is the last term, I found no way of removing the double quotes to the number, is it possible in linux?

This is what I tried:

#!/usr/bin/python

import os,sys,re,string,array

name=sys.argv[1]
infile = open(name,"r")

cont = 0
while 1:
         line = infile.readline()
         if not line: break
         l = re.split("\s+",string.strip(line)).replace('\"','')
     cont = cont +1
     a = l[0]
     b = l[1]
     c = l[2]
     d = l[3]
+9  A: 
for line in open(name, "r"):
    line = line.replace('"', '').strip()
    a, b, c, d = map(float, line.split())

This is kind of bare-bones, and will raise exceptions if (for example) there aren't four values on the line, etc.

Ned Batchelder
Is there any reason why this is preferable to using a built in module for this purpose, as I've shown in my answer?
abyx
`shlex` is quite specialized. It happens to work perfectly for this task, but it may be more important for the OP to learn some of the more basic and more flexible tools first.
Ned Batchelder
+4  A: 
for line in open(fname):
    line = line.split()
    line[-1] = line[-1].strip('"\n')
    floats = [float(i) for i in line]

another option is to use built-in module, that is intended for this task. namely csv:

>>> import csv
>>> for line in csv.reader(open(fname), delimiter=' '):
    print([float(i) for i in line])

[5.6, 4.5, 6.8, 6.5]
[5.6, 4.5, 6.8, 6.5]
SilentGhost
+1 didn't know csv strips quotes
abyx
it can do it in different manner too: http://docs.python.org/library/csv.html#csv.QUOTE_ALL
SilentGhost
A: 

You can use regexp, try something like this

import re
re.findall("[0-9.]+", file(name).read())

This will give you a list of all numbers in your file as strings without any quotes.

Serge
+2  A: 

Or you can simply replace your line

l = re.split("\s+",string.strip(line)).replace('\"','')

with this:

l = re.split('[\s"]+',string.strip(line))
yu_sha
hi, thanks, this is the best approach I found for my problem
Werner
+6  A: 

There's a module you can use from the standard library called shlex:

>>> import shlex
>>> print shlex.split('5.6  4.5  6.8  "6.5"')
['5.6', '4.5', '6.8', '6.5']
abyx