python - 为什么 .readlines() 会生成单个字符的列表?

标签 python list io split

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

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

我做错了什么?

最佳答案

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

关于python - 为什么 .readlines() 会生成单个字符的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16614861/

相关文章:

python - 获取 IdleX AttributeError

网页设计师的 Python 模板

python - 将元组列表转换为字典

scala - 在没有错误状态的情况下处理 iteratee 库中的异常

java - 下面的代码片段中的 "copy ByteBuffer"代表什么?

python - 如何阻止 Python unittest 打印测试文档字符串?

java - Java中等效的生成器函数

python - 如何将一个词典列表添加到另一个词典列表?

python - 如何将杂乱的字典扁平化为列表?

python - 围绕文件对象堆叠过滤器