2

我有一个这种格式的文本文件:

EFF   3500.  GRAVITY 0.00000  SDSC GRID  [+0.0]   VTURB 2.0 KM/S    L/H 1.25                            
  wl(nm)    Inu(ergs/cm**2/s/hz/ster) for 17 mu in 1221 frequency intervals
            1.000   .900  .800  .700  .600  .500  .400  .300  .250  .200  .150  .125  .100  .075  .050  .025  .010
    9.09 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.35 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.61 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.77 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.96 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
   10.20 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
   10.38 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

...more numbers 

我正在努力做到这一点,所以File[0][0]会打印“EFF”等字样。

import sys
import numpy as np
from math import *
import matplotlib.pyplot as plt

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

z = np.array(sys.argv)          #store all of the file names into array

i = len(sys.argv)           #the length of the filenames array

File = open(str(z[1])).readlines()  #load spectrum file 

for n in range(0, len(File)):
    File[n].split()

for n in range(0, len(File[1])):
    print File[1][n]

但是,它会一直输出单个字符,就好像每个列表索引都是单个字符一样。这也包括空格。我在一个循环中有 split() ,因为如果我把 readlines().split() 放入它会给出一个错误。

输出:

    E
    F
    F



    3
    5
    0
    0
    .


    G
    R
    A
    V
    I

...ect

我究竟做错了什么?

4

2 回答 2

4
>>> text = """some
... multiline
... text
... """
>>> lines = text.splitlines()
>>> for i in range(len(lines)):
...     lines[i].split()  # split *returns* the list of tokens
...                       # it does *not* modify the string inplace
... 
['some']
['multiline']
['text']
>>> lines   #strings unchanged
['some', 'multiline', 'text']
>>> for i in range(len(lines)):
...     lines[i] = lines[i].split() # you have to modify the list
... 
>>> lines
[['some'], ['multiline'], ['text']]

如果你想要一个单线做:

>>> words = [line.split() for line in text.splitlines()]
>>> words
[['some'], ['multiline'], ['text']]

使用文件对象应该是:

with open(z[1]) as f:  
    File = [line.split() for line in f]

顺便说一句,您在循环时使用了反成语。如果您想循环遍历一个可迭代对象,只需执行以下操作:

for element in iterable:
    #...

如果您还需要元素的索引,请使用enumerate

for index, element in enumerate(iterable):
    #...

在你的情况下:

for i, line in enumerate(File):
    File[i] = line.split()

for word in File[1]:
    print word
于 2013-05-17T17:37:54.660 回答
1

你想要这样的东西:

for line in File:
    fields = line.split()
    #fields[0] is "EFF", fields[1] is "3500.", etc.

split() 方法返回一个字符串列表,它不会修改被调用的对象。

于 2013-05-17T17:43:02.620 回答