csv文件转成txt文件

csv文件转成txt文件

借鉴文章https://blog.csdn.net/lqzdreamer/article/details/76549256#commentBox

今天在跑svm小实验的时候,要把csv文件转成txt文件的时候遇到了困难,先把csv文件另存为txt格式的,如果代码是utf-8记得选utf-8格式,我把它保存成utf-8的格式后,导入的txt文件,代码仍然报错,代码中导入文件的代码本来是这样的,

filename_traindata ='E:/project/datasvm/train_data.tx'
filename_testdata = 'E:/project/datasvm/test_data.txt'

即使我把它改成这样还是不行,

filename_traindata=open("E:\project\datasvm\\train_data.txt",'r',encoding="gbk",errors='ignore').read()
filename_testdata=open("E:\project\datasvm\\test_data.txt",'r',encoding="gbk",errors='ignore').read()

后来我对我的txt文件重新进行了读取,然后把生成的txt文件再次放到代码里,程序就可以成功跑了,代码如下,

path = 'E:\project\datasvm\\test.txt'
file = open(path, encoding='gb18030', errors='ignore')
file2 = open('E:\project\datasvm\\ccc.txt', 'w')
file2.write(file.read())
file.close()
file2.close()
with open('E:\project\datasvm\\ccc.txt', 'r') as text:
    words = text.read().split()
    print(words)
    for word in words:
        print('{}-{} times'.format(word, words.count(word)))

 

filenam

filename_traindata=open("E:\project\datasvm\\train_data.txt",'r',encoding="gbk",errors='ignore').read()
filename_testdata=open("E:\project\datasvm\\test_data.txt",'r',encoding="gbk",errors='ignore').read()

e_traindata=open("E:\project\datasvm\\train_data.txt",'r',encoding="gbk",errors='ignore').read()
filename_testdata=open("E:\project\datasvm\\test_data.txt",'r',encoding="gbk",errors='ignore').read()

filename_traindata=open("E:\project\datasvm\\train_data.txt",'r',encoding="gbk",errors='ignore').read()
filename_testdata=open("E:\project\datasvm\\test_data.txt",'r',encoding="gbk",errors='ignore').read()

filename_traindata=open("E:\project\datasvm\\train_data.txt",'r',encoding="gbk",errors='ignore').read()
filename_testdata=open("E:\project\datasvm\\test_data.txt",'r',encoding="gbk",errors='ignore').read()

你可能感兴趣的:(csv文件转成txt文件)