could not convert string to float解决savetxt()数据导出的问题

import numpy as np
import pandas as pd
from numpy import loadtxt
from sklearn.feature_selection import RFECV
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import KFold
import numpy as np



file = open("D:\JMDS.csv","r")
list_arr = file.readlines()

lists = []

for index,x in enumerate(list_arr):
    x = x.replace('A','1')
    x = x.replace('B','2')
    x = x.replace('C','3')
    x = x.replace('D','4')
    x = x.replace('E','5')
    x = x.replace('F','6')
    x = x.replace('G','7')
    x = x.replace('H','8')
    x = x.replace('I','9')
    x = x.replace('J','10')
    x = x.replace('K','11')
    x = x.replace('L','12')
    x = x.replace('M','13')
    x = x.replace('N','14')
    x = x.replace('O','15')
    x = x.replace('P','16')
    x = x.replace('Q','17')
    x = x.replace('R','18')
    x = x.replace('S','19')
    x = x.replace('T','20')
    x = x.replace('U','21')
    x = x.replace('V','22')
    x = x.replace('W','23')
    x = x.replace('X','24')
    x = x.replace('Y','25')
    x = x.replace('Z','26')
    x = x.replace('a','1')
    x = x.replace('b','2')
    x = x.replace('c','3')
    x = x.replace('d','4')
    x = x.replace('e','5')
    x = x.replace('f','6')
    x = x.replace('g','7')
    x = x.replace('h','8')
    x = x.replace('i','9')
    x = x.replace('j','10')
    x = x.replace('k','11')
    x = x.replace('l','12')
    x = x.replace('m','13')
    x = x.replace('n','14')
    x = x.replace('o','15')
    x = x.replace('p','16')
    x = x.replace('q','17')
    x = x.replace('r','18')
    x = x.replace('s','19')
    x = x.replace('t','20')
    x = x.replace('u','21')
    x = x.replace('v','22')
    x = x.replace('w','23')
    x = x.replace('x','24')
    x = x.replace('y','25')
    x = x.replace('z','26')
    x = x.replace('_','0')
    x = x.replace(' ', '')
    x = x.replace(',,', ',0,')
    x = x.replace(',', '')
   lists.append(x)
a = np.array(lists)
a = a.astype(str)
print (a)
file.close()

np.savetxt('d:/new6.csv',a,delimiter = ',',fmt = '%s',newline='')

在载入数据的时候各种字符串无法载入的问题
问题1,could not convert string to float:
空白无法载入到字符串,
如果csv里有空格就无法转换成字符串
解决办法:x = x.replace(‘,,’, ‘,0,’)
将空值转换成0,
fmt = ‘%s’
问题2,导出的csv每一行之间有空格,在savetxt里加上newline=”解决这个问题

你可能感兴趣的:(学习,python,numpy,数据导出,字符串)