Python float警告,删除命令

2024-10-03 13:27:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,

我知道这个价格:1304.20-1500.20-4.10-140.50
如何删除逗号?你知道吗

for open_price in df_open:
    if ',' in open_price:
        open_priceClean = open_price.replace(',', '')
        continue
    prices.append(float(open_priceClean))

我得到这个警告:

TypeError
Traceback (most recent call last) <ipython-input-67-80afd251c13d> in <module>()
     10 #Create the dependent data set 'y' as prices
     11 for open_price in df_open:
---> 12     if ',' in open_price:
     13         open_priceClean = open_price.replace(',', '')
     14         continue

TypeError: argument of type 'float' is not iterable

Tags: indfforif价格openfloatprice
1条回答
网友
1楼 · 发布于 2024-10-03 13:27:54

试试这个:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
df_open = open("test.txt","r")
read_file = df_open.read().splitlines()
print (read_file)

for line in read_file:
    no_comas = (line.replace(",",""))
    print (no_comas)

相关问题 更多 >