Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print line line=data.readline() (2)一次全部读入内存 data=open("data.txt") for line in data.readlines(): print line
# 'C:\Users\SAM\Desktop\数据竞赛\个人征信_1108\个人征信\train\bank_detail_train.txt'# 反斜杠的写法会报编码错误f=open('C:/Users/SAM/Desktop/数据竞赛/个人征信_1108/个人征信/train/bank_detail_train.txt')result= []iter_f=iter(f) #用迭代器循环访问文件中的每一行for line in iter_f: result.append(line)f.clos
方法和画折线图类似,差别在于画图函数不一样,用的是scatter() import matplotlib.pyplot as plt #以外部两个txt表分别作为x,y画图n=0m=0with open(r'C:\Users\Administrator\Desktop\test.txt') as text: a=text.readlines()with open(r'C:\Users\Administrator\Desktop\tet.txt') as tet: month=tet.
读数据代码: with open(path,'r') as f: for line in f: line = line.strip() 报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 451428: illegal multibyte sequence 尝试修改代码为: with open(path,encoding="UTF-8") 又报其他错误: UnicodeDecodeError: '
import xlwt import os def write_excel(words,filename): #写入Excel的函数,words是数据,filename是文件名 wb=xlwt.Workbook() sheet=wb.add_sheet('sheet1') attr=['词语','词性','词频'] #第一行:属性行 for col in range(3): sheet.write(0,col,attr[col]) for row in range(1,len(words)+1)