24小时热门版块排行榜    

Znn3bq.jpeg
查看: 6272  |  回复: 5
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

lanmoa

新虫 (初入文坛)

[求助] 新手导入csv过程中总是出现python could not convert string to float 已有1人参与

程序:
image_path = r'C:\Users\abc\Documents\csv_result\cracking\41.csv'
with open(image_path,'r') as image_file:
    image_string = image_file.read()
image_string = image_string.replace(' ','').replace('\n',',').replace('\t','')
print(image_string)
image_data = image_string.split(',')
print(image_data)
image_data_values = [float(x) for x in image_data]
print (image_string)
前两个结果1
-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.26,-11.2
结果2
'-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26', '-11.26',
然后报错
ValueError                                Traceback (most recent call last)
<ipython-input-27-225cd40ddc4d> in <module>()
      6 image_data = image_string.split(',')
      7 print(image_data)
----> 8 image_data_values = [float(x) for x in image_data]
      9 print (image_string)

<ipython-input-27-225cd40ddc4d> in <listcomp>(.0)
      6 image_data = image_string.split(',')
      7 print(image_data)
----> 8 image_data_values = [float(x) for x in image_data]
      9 print (image_string)

ValueError: could not convert string to float:
为什么?
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

me_yu

木虫 (正式写手)


jjdg: 金币+1, 感谢参与 2017-08-26 15:42:32
为什么不用pandas来处理呢
听,寂静的夜里,田野里蛙声和虫鸣,好不欢快!
5楼2017-08-25 11:57:20
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 6 个回答

somomo91

专家顾问 (职业作家)

【答案】应助回帖


感谢参与,应助指数 +1
xzhdty: 金币+1, 感谢参与 2017-06-23 11:11:00
你的 image_data 文件里面包含 空格/空字符,所以 float 错误,
Python 3 会出现这个问题,2 好像没有
2楼2017-06-22 19:16:29
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cxycxu

新虫 (初入文坛)

引用回帖:
2楼: Originally posted by somomo91 at 2017-06-22 19:16:29
你的 image_data 文件里面包含 空格/空字符,所以 float 错误,
Python 3 会出现这个问题,2 好像没有

您好,我是一个初接触Python的新手,想问您一下,如何读取res格式的文件
3楼2017-06-26 14:02:55
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cxycxu

新虫 (初入文坛)

你好  有一些Python的问题可以问你吗  读入res格式的文件到底应该怎么做呢
4楼2017-06-26 14:03:57
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 材料专硕(0856) 339分求调剂 +6 哈哈哈鹅哈哈哈 2026-04-09 7/350 2026-04-09 13:02 by gong120082
[考研] 调剂 化学 307 +10 73372112 2026-04-09 11/550 2026-04-09 12:14 by 418490947
[考研] 22408 270分 +7 sanjin020722 2026-04-08 8/400 2026-04-09 11:23 by 求调剂zz
[考研] 一志愿2110,化学学硕310分,本科重点双非求调剂 +15 努力奋斗112 2026-04-08 15/750 2026-04-09 11:22 by lenovolsw
[考研] 296求调剂 +3 汪!?! 2026-04-08 3/150 2026-04-08 21:46 by 土木硕士招生
[考研] 考研求调剂 +4 雯??? 2026-04-08 4/200 2026-04-08 21:44 by 土木硕士招生
[考研] 270求调剂 +3 031127 2026-04-06 4/200 2026-04-08 21:00 by 逆水乘风
[考研] 305分求调剂 +3 哈_哈_哈_哈_哈 2026-04-04 5/250 2026-04-07 14:49 by 哈_哈_哈_哈_哈
[考研] 0854求调剂 +9 亨氏番茄沙司 2026-04-06 10/500 2026-04-07 14:37 by shdgaomin
[考研] 一志愿苏州大学材料工程(085601)专硕有科研经历三项国奖两个实用型专利一项省级立项 +11 大火山小火山 2026-04-05 11/550 2026-04-06 22:55 by yunlongyang
[考研] 287求调剂 +3 通信学硕081000 2026-04-03 4/200 2026-04-06 21:03 by going home
[考研] 考研调剂 +3 Wwwwwww哇 2026-04-06 3/150 2026-04-06 20:55 by lbsjt
[考研] 277求调剂 +4 12A3 2026-04-02 5/250 2026-04-04 20:28 by 蓝云思雨
[考研] 材料383求调剂 +5 郭阳阳阳成 2026-04-04 5/250 2026-04-04 19:06 by dongzh2009
[考研] 复试调剂 +6 范根培 2026-04-04 6/300 2026-04-04 14:27 by 土木硕士招生
[考研] 322求调剂 +6 FZAC123 2026-04-03 6/300 2026-04-03 22:23 by 科研小专家
[考研] 本科985,专业0812分336求调剂 +4 莫莫很行 2026-04-03 4/200 2026-04-03 21:31 by zhq0425
[考研] 283求调剂 +3 jiouuu 2026-04-03 4/200 2026-04-03 13:28 by jiouuu
[考研] 303求调剂 +3 一色清羽 2026-04-02 4/200 2026-04-03 10:22 by 蓝云思雨
[考研] 求调剂 +7 Aniyaio 2026-04-02 7/350 2026-04-02 16:42 by zzsw+
信息提示
请填处理意见