【发布时间】:2020-10-06 18:56:25
【问题描述】:
#read the data from the file
#takes in file name
def readFile(fileName):
myFile=open(fileName,"r")
#reading the lines in the file and returning the data skips the first line
fileData=myFile.readlines()[1:]
myFile.close()
myNewList = []
numRow = 0
numCol = 0
emptyLines = 0
#from 0 to the length of the file data list
for i in range(0,len(fileData),1):
#creating a empty list for each row of data if the line is not empty
if fileData[i].strip() != "":
myNewList.append([])
#checks for empty lines
if fileData[i].strip() == "":
emptyLines = emptyLines + 1
numRow = numRow + 1
#if file data at a certain index with no spaces dosen't equal a empty line
if fileData[i].strip() != NEWLINE:
#removing spaces and splitting data by spaces
myElements=fileData[i].strip().split(SPACE)
myElements=fileData[i].split()
for j in range(0,len(myElements),1):
numCol = len(myElements)
if myElements[j].strip() != SPACE:
myNewList[i].append(int(myElements[j]))
#calculating and displaying the total
def display(myList,numRow,numCol):
total = 0
#loops 7 times for just the first 7 numbers but in every row
for i in range(0,numRow,1):
total = 0
for j in range(0,numCol,1):
if j < 7:
if myList[i][j] != "":
print("%s" %myList[i][j], end = "-")
total = total + myList[i][j]
print("Total=%s"%total)
我需要做些什么来排除没有空格的空行被添加到二维数组中。如果文本文件底部有空行但中间没有空行,我有办法将它们排除在外。我基本上希望程序不包含空行
【问题讨论】: