开发者

Python String Manipulation

开发者 https://www.devze.com 2023-02-07 07:33 出处:网络
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?

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开发者_如何学Go.07053968550834916,0
0.03125,-0.09041720958299812,0
0.03515625,-0.28273374252040306,0
0.0390625,-0.29775398016603216,0


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])


with open("input.txt") as f:
    for s in f:
        print(s.split(",")[1])


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.


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 ;-) )


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]
0

精彩评论

暂无评论...
验证码 换一张
取 消