python读取txt文本文件
if __name__ == "__main__": filename = 'innerIdAndType.txt' # txt文件和当前脚本在同一目录下,所以不用写具体路径 postIdList = [] commentIdListOne = [] commentIdListTwo = [] count = 0 with open(filename, 'r') as file_to_read: while True: line = file_to_read.readline() # 整行读取数据 if not line: break line = str.replace(line,"\n", "") res = str.split(line, '\t') type = res[1] idstr = res[0] if int(type) == 0: postIdList.append(idstr) elif int(type) == 1: count = count + 1 if count > 200: commentIdListTwo.append(idstr) else: commentIdListOne.append(idstr) continue