Python 2.7 Python 2.7.3合并排序接收--”;TypeError:类型为'的对象;文件';没有len();

Python 2.7 Python 2.7.3合并排序接收--”;TypeError:类型为'的对象;文件';没有len();,python-2.7,typeerror,mergesort,Python 2.7,Typeerror,Mergesort,我刚写完我的第一个mergesort程序,在编译时遇到了麻烦。我已经对这个特定的错误做了大量的研究,似乎我在代码中的某个地方是非特定的。我仍然找不到所说的错误,希望您能帮助我。我已附上文件内容、代码和回溯。再次感谢 文件: 代码: DEBUG=True out=[] logs=open(“C:\Users\---\Desktop\logs.txt”,mode=“r”) lines=logs.readline() def调试: 如果调试: 打印“调试:”,s def get_t(行): s=直线

我刚写完我的第一个mergesort程序,在编译时遇到了麻烦。我已经对这个特定的错误做了大量的研究,似乎我在代码中的某个地方是非特定的。我仍然找不到所说的错误,希望您能帮助我。我已附上文件内容、代码和回溯。再次感谢

文件:

代码:

DEBUG=True
out=[]
logs=open(“C:\Users\---\Desktop\logs.txt”,mode=“r”)
lines=logs.readline()
def调试:
如果调试:
打印“调试:”,s
def get_t(行):
s=直线
s=s.lstrip()
调试
i=s.find(“”)
调试
s=s[:i]
返回整数(s)
def get_最低_i(日志):
最低_i=-1
对于范围内的i(len(logs)):
log=logs[i]
调试(“log=“+repr(log))
如果记录:
t=获取(日志[0])
调试(“t=“+repr(t))
如果最低值i==-1或t<最低值t:
最低_i=i
最低t=t
返回最低值
def get_line_lower_t(日志):
尽管如此:
i=获取最低值(日志)
如果i==-1:
打破
line=logs[i].pop(0)
def合并排序(日志):
尽管如此:
行=获取行最低值(日志)
如果行==无:
打破
out.append(行)
返回
打印合并排序(日志)
f、 关闭()
回溯:

Traceback (most recent call last):
  File "<module1>", line 50, in <module>
  File "<module1>", line 44, in mergesort
  File "<module1>", line 37, in get_line_lowest_t
  File "<module1>", line 24, in get_lowest_i
TypeError: object of type 'file' has no len()
回溯(最近一次呼叫最后一次):
文件“”,第50行,在
文件“”,第44行,合并排序
文件“”,第37行,在获取行中
文件“”,第24行,在get_\u i中
TypeError:类型为“file”的对象没有len()

提前感谢。

您正在合并排序文件,而不是名为
行的数组。

类型错误:“file”类型的对象没有len()
错误表明您正在尝试读取文件对象的长度。。。由于
logs=open(“C:\Users\---\Desktop\logs.txt”,mode=“r”)
是一个文件,您可能想读取文件的行并对其排序
lines=longs.readlines()
打印合并排序(行)
文件没有方法len()。将其放入字符串或数组中,然后使用len()

DEBUG = True
out = []

logs = open("C:\Users\----\Desktop\logs.txt", mode ="r")

lines = logs.readline()

def debug(s):
    if DEBUG:
        print "DEBUG: ", s

def get_t (line):
    s = line
    s = s.lstrip()
    debug(s)
    i = s.find(" ")
    debug(s)
    s = s[:i]
    return int(s)

def get_lowest_i(logs):
    lowest_i = -1
    for i in range(len(logs)):
        log = logs[i]
        debug("log=" + repr(log))
        if log:
            t = get_t(log[0])
            debug("t=" + repr(t))
            if lowest_i == -1 or t < lowest_t:
                lowest_i = i
                lowest_t = t
    return lowest_i

def get_line_lowest_t(logs):
    while True:
        i = get_lowest_i(logs)
        if i == -1:
            break
        line = logs[i].pop(0)

def mergesort(logs):
    while True:
        line = get_line_lowest_t(logs)
        if line == None:
            break
        out.append(line)
    return out

print mergesort(logs)

f.close()
Traceback (most recent call last):
  File "<module1>", line 50, in <module>
  File "<module1>", line 44, in mergesort
  File "<module1>", line 37, in get_line_lowest_t
  File "<module1>", line 24, in get_lowest_i
TypeError: object of type 'file' has no len()