2012-02-28 80 views
1

一个新的.txt文件我想输入或读取.txt文件的以下数据:输入的数据文件.txt和输出特定的数据使用Python

VIBRATION/SHOCK CALIBRATION DATA 
DATE: 2/26/2012 
TIME: 0800 
Time (ms) Channel 1 Channel 2  Channel 3 
0.0   -0.9   9.0   12.9 
50.0  5.0   12   343 
100.0  56.7   120   0.8 
150.0  90.0   0.9   12.0 
200.0  32.9   12.4   34.0 

然后输出到一个新的。 txt文件,使得只有数字的时间和频道3个栏写着:

0.0  12.9 
50.0 343 
100.0 0.8 
150.0 12.0 
200.0 34.0 
+0

我使用了一个计数器为只读下面的时间,然后输出到一个文件,但不知道该命令只提取通道3 – guiNachos 2012-02-28 17:29:58

回答

0

你可以打电话就行字符串分割()来获取列:

t, ch1, ch2, ch3 = line.split() 

返回的值将是字符串。如果你想要的数字,使用浮动(),将它们转换:

t_f = float(t) 
ch3_f = float(ch3) 
3

作为一个完整的例子,可以考虑像下面的代码。我已经添加了过多的评论来解释每一步正在做什么。

# Open the input file read-only... 
with open('infile.txt', 'r') as infile: 
    # Skip the first 4 lines, and store them as "header" in case we need them... 
    # It's important that we use "next" here and _not_ infile.readline(). 
    # readline and the file iteration methods ("for line in file") can't be mixed 
    header = [next(infile) for dummy in range(4)] 

    # Open the output file, overwriting whatever is there... 
    with open('outfile.txt', 'w') as outfile: 
     # Loop over the lines in the input file 
     for line in infile: 
      # Strip off leading and trailing whitespace (e.g "\n") and 
      # split on whitespace. This gives us a list of strings. 
      columns = line.strip().split() 
      # Write the 1st and 4th columns in each row as left-justified 
      # columns with a fixed-width of 8 
      outfile.write('{:8}{:8}\n'.format(columns[0], columns[3])) 

如果您使用的是旧版本的Python,并希望避免with语句,你可以写这样的:

# Open the input file read-only... 
infile = open('infile.txt', 'r') 
# Open the output file, overwriting whatever is there... 
outfile = open('outfile.txt', 'w') 

# Skip the first 4 lines, and store them as "header" in case we need them... 
# It's important that we use "next" here and _not_ infile.readline(). 
# readline and the file iteration methods ("for line in file") can't be mixed 
header = [next(infile) for dummy in range(4)] 

# Loop over the lines in the input file 
for line in infile: 
    # Strip off leading and trailing whitespace (e.g "\n") and 
    # split on whitespace. This gives us a list of strings. 
    columns = line.strip().split() 
    # Write the 1st and 4th columns in each row as left-justified 
    # columns with a fixed-width of 8 
    outfile.write('{:8}{:8}\n'.format(columns[0], columns[3])) 

# Close the file objects once we're through with them.. 
# Python would close these automatically when the process ends, but it's a good 
# idea to get in the habit of explicitly closing them once you don't need them. 
# Otherwise, when you start writing code for longer-running processes, you'll 
# inadvertently leave lots of file handles laying around 
infile.close() 
outfile.close() 

然而,这是一个好主意,让成习惯使用with语句来处理文件对象。他们确保即使代码中有错误,文件句柄也会自动关闭。 with语句是“上下文管理器”。他们对很多事情本来需要“样板”的清理和/或进入代码非常方便。

+1

我想补充一点,你应该使用'rb'和'wb'(读取和写入的二进制分别),因为它具有比默认的'r'更少的跨平台问题和'w'模式。 – Edwin 2012-02-28 18:04:43

+0

感谢您的反馈。更有意义。 – guiNachos 2012-02-28 19:01:48