-1

I have a file that looks like the following. All I want is the Voltage, what is easiest way to strip everything else from it?

Time,Voltage,Peak
0.0,1.003911558621642,3
0.00390625,1.0327467181982755,0
0.0078125,0.9904463156237306,0
0.01171875,0.6867661682528724,0
0.015625,0.6236803073669519,0
0.01953125,0.2934711210503298,0
0.0234375,0.06148933838536881,0
0.02734375,0.07053968550834916,0
0.03125,-0.09041720958299812,0
0.03515625,-0.28273374252040306,0
0.0390625,-0.29775398016603216,0
martineau
  • 119,623
  • 25
  • 170
  • 301
patrick
  • 1,022
  • 5
  • 17
  • 29

5 Answers5

8

This sound a job to the csv module

import csv
with open("input.txt", "rb") as f:
    reader = csv.reader(f)
    next(reader)
    for i in reader:
        print float(i[1])
razpeitia
  • 1,947
  • 4
  • 16
  • 36
4
with open("input.txt") as f:
    for s in f:
        print(s.split(",")[1])
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • -1 (I would but i cannot because i already downvoted and then upvoted it): This solution is not skipping the heading line and it's not converting the voltage to a float – 6502 Jan 30 '11 at 18:57
  • @6502: Why would you convert to float? There's nothing about that in the question. – viraptor Jan 31 '11 at 03:51
  • @viraptor: I inferred that because the original poster wrote "All I want is the Voltage" and neither "All I Want is the second column" nor "All I Want are the Voltage fields". So, IMO, the "Voltage" heading is not needed and a reasonable data type to represent the voltage (e.g. to make a graph) is float. May be of course that you're right and I'm completely wrong, however what I see here is just another case of the Mattew's effect ( http://goo.gl/O4kFJ ) i.e. whatever a 126k says is going to look "more correct" no matter what. – 6502 Jan 31 '11 at 07:35
  • @6502: I didn't assume anything about what the OP wanted to actually use the data in the second column for. While your answer is also perfectly correct (I'll +1 it), a single-expression answer is perhaps less accessible for somebody who's clearly a Python beginner. – Greg Hewgill Jan 31 '11 at 18:31
  • You're right and I felt guilty. I added as a self punishment a much more detailed explanation of that one liner. Still I think that inferring **real** requirements is an important art, but you had a serious point. – 6502 Jan 31 '11 at 19:21
4

This will skip the first line and return a list of floats from the second column:

def get_second_col_floats(file_name):
    with open(file_name) as f:
        f.next() # skip the first line
        return [float(line.split(',')[1]) for line in f]

Edit: You also may want to check the Python CSV module out if you end up needing to do more advanced things. It's part of the standard library so it won't add more dependencies.

mikelikespie
  • 5,682
  • 3
  • 31
  • 36
3

This will return voltage as a list of float values

voltage = [float(x.split(",")[1])
           for x in open("input.txt").readlines()[1:]]

This compact form takes advantage of many Python features

  1. open("input.txt").readlines() is a single expression that returns you the whole content of a file as a list where each element is one line from the file. Putting the whole file in a list is a reasonable way to process the content of a file unless the size of the file is huge (several megabytes) and processing it explicitly on a line-by-line basis is better.

  2. x[1:] is a Python "slice" expression that given a list x returns a new identical list with however the first element from x removed. In your case it's used to drop the heading line.

    The general form is x[begin:end:step] and allows to extract data from lists in many useful ways... e.g. the list of all elements of x with an even index is just x[::2], or the list of last 10 elements of x is x[-10:].

  3. x.split(",") returns the content of the string x as an array of substrings by cutting on the specified separator ",". So the combined expression x.split(",")[1] allows the extraction of the second value from a line of the file.

  4. [expr(x) for x in L] is named "list comprehension" expression and returns the list of the result of evaluating expr(x) for each item present in the list L.

All of them combined those features allows to solve your problem in just a single line of code, and while this is OK for small problems like this one it's however something that shouldn't be pushed to the extreme (unless you're playing golf ;-) )

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265
2

You could use a combination of the csv module and a list comprehension to store all the floating point voltage values in a list for further processing. The list is created in the context of a with statement, which will automatically take care of closing the file afterwards, even if an error occurs.

Data from the file is processed by reading it in one line at a time rather than all of it at once, which minimizes memory use during construction of the list regardless of the size file. It would be very easy to extend this to handle the other values and store them in the list, too, or another type of data structure, such as a dictionary.

import csv
with open("data.txt", "rb") as csvfile:
    voltages = [float(row['Voltage']) for row in csv.DictReader(csvfile)]

print 'voltages:', voltages

Output:

voltages: [1.003911558621642, 1.0327467181982755, 0.9904463156237306, 0.6867661682528724, 0.6236803073669519, 0.2934711210503298, 0.06148933838536881, 0.07053968550834916, -0.09041720958299812, -0.28273374252040306, -0.29775398016603216]
martineau
  • 119,623
  • 25
  • 170
  • 301