Python 将特定列中的数字乘以因子3

Python 将特定列中的数字乘以因子3,python,python-3.x,numpy,Python,Python 3.x,Numpy,我想将特定列(以下输入文件xyz.txt中的z列)中的所有数字乘以因子3,并将其输出到文本文件中。当我运行/script.py xyz.txt>output.txt时,我得到以下错误: 回溯(最后一次调用):文件“/script.py”,第23行,在 sys.exit(main(sys.argv))文件“/script.py”,main中的第18行 result.append(z.split(“”)[2]*3)索引器:列表索引超出范围 你知道我如何修复这个错误吗 script.py: #!/us

我想将特定列(以下输入文件xyz.txt中的z列)中的所有数字乘以因子3,并将其输出到文本文件中。当我运行
/script.py xyz.txt>output.txt
时,我得到以下错误:

回溯(最后一次调用):文件“/script.py”,第23行,在 sys.exit(main(sys.argv))文件“/script.py”,main中的第18行 result.append(z.split(“”)[2]*3)索引器:列表索引超出范围

你知道我如何修复这个错误吗

script.py:

#!/usr/bin/env python3

def main(argv):

    inputfile = open(argv[1])

    line = inputfile.readline()

    while line:
        print(line, end="")
        if line.startswith('[ xyz ]'):
            break
        line = inputfile.readline()

    result=[]
    for z in line:
        result.append(z.split(' ')[2]*3)
        print(z.rstrip(), '; modified')

if __name__ == "__main__":
    import sys
    sys.exit(main(sys.argv))
[ xyz ]
;     x     y      z  
     1.5   3.5     6.3
     2.4   4.2     2.4
     3.2   8.9     8.9
     4.3   2.1     9.2
     5.4   6.3     3.5 
[ xyz ]
;     x     y      z  
     1.5   3.5    18.9 ; modified
     2.4   4.2     7.2 ; modified
     3.2   8.9    26.7 ; modified
     4.3   2.1    27.6 ; modified
     5.4   6.3    10.5 ; modified
输入文件xyz.txt:

#!/usr/bin/env python3

def main(argv):

    inputfile = open(argv[1])

    line = inputfile.readline()

    while line:
        print(line, end="")
        if line.startswith('[ xyz ]'):
            break
        line = inputfile.readline()

    result=[]
    for z in line:
        result.append(z.split(' ')[2]*3)
        print(z.rstrip(), '; modified')

if __name__ == "__main__":
    import sys
    sys.exit(main(sys.argv))
[ xyz ]
;     x     y      z  
     1.5   3.5     6.3
     2.4   4.2     2.4
     3.2   8.9     8.9
     4.3   2.1     9.2
     5.4   6.3     3.5 
[ xyz ]
;     x     y      z  
     1.5   3.5    18.9 ; modified
     2.4   4.2     7.2 ; modified
     3.2   8.9    26.7 ; modified
     4.3   2.1    27.6 ; modified
     5.4   6.3    10.5 ; modified
请求的输出文件output.txt:

#!/usr/bin/env python3

def main(argv):

    inputfile = open(argv[1])

    line = inputfile.readline()

    while line:
        print(line, end="")
        if line.startswith('[ xyz ]'):
            break
        line = inputfile.readline()

    result=[]
    for z in line:
        result.append(z.split(' ')[2]*3)
        print(z.rstrip(), '; modified')

if __name__ == "__main__":
    import sys
    sys.exit(main(sys.argv))
[ xyz ]
;     x     y      z  
     1.5   3.5     6.3
     2.4   4.2     2.4
     3.2   8.9     8.9
     4.3   2.1     9.2
     5.4   6.3     3.5 
[ xyz ]
;     x     y      z  
     1.5   3.5    18.9 ; modified
     2.4   4.2     7.2 ; modified
     3.2   8.9    26.7 ; modified
     4.3   2.1    27.6 ; modified
     5.4   6.3    10.5 ; modified

你好像在分裂一条像新线一样的线。此外,您应该使用
csv
模块来读取更适合此类任务的文件。或者,作为一种更好的方法,您可以使用
numpy
pandas
modul。您可以执行
if(len(z.split(“”)>2):result.append(z.split(“”)[2]*3)
@erhan:您必须以txt格式转储文件。您还需要管理列表中z值的索引。