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

5 篇文章 0 订阅
2 篇文章 0 订阅
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=”解决这个问题

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值