简体   繁体   English

python代码末尾的“ None”打印

[英]“None” print at end of python code

I'm trying to get a python program to take a .dat file and read it into a matrix. 我正在尝试获取一个python程序来获取.dat文件并将其读入矩阵。 I've it working for the most part, however "None" keeps printing at the end of the statement. 我大部分时间都在使用它,但是“ None”始终在语句末尾打印。

import sys

def main():

  matrix = generateMatrixFromFile(sys.argv[1])

  print(printMatrix(matrix))

def generateMatrixFromFile(fname):
  fp = open(fname, "r")
  row = fp.readlines()
  matrix = []
  for i in range(0, len(row), 1):
    token = row[i].split(" ")
    token[-1] = token[-1].replace('\n', '')
    matrix.append(token)
  fp.close()
  return matrix

def printMatrix(matrix):
  for i in range(0, len(matrix), 1):
    for j in range(0, len(matrix[i]), 1):
      print(matrix[i][j], sep=" ", end=" ")
    print()

main()

This line: 这行:

print(printMatrix(matrix)) 

is printing the return value of your function, which is None . 正在打印函数的返回值,即None

Just calling printMatrix(matrix) should make the result as you expected. 只需调用printMatrix(matrix)获得预期的结果。

Remove the print on this line: 删除此行上的print

print(printMatrix(matrix))

Having it makes Python print the return value of printMatrix , which is None . 有了它,Python将打印printMatrix的返回值,即None

您的方法printMatrix(matrix)返回任何内容( None ),并在其上调用打印( print(printMatrix(matrix))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM