英文:
how to transform strings in csv file into float and store them as a list?
问题
我有一个看起来像这样的CSV文件:
id,type,inc,a,b
1,2,63376.799999999996,0.3061,187
2,1,58087.700000000004,0.3361,178
3,1,52699.9,0.3482,181
4,2,72964.8,0.3186,177
5,4,111589.79999999999,0.268,154
6,4,107618.0,0.2583,150
7,2,87109.2,0.3233,193
8,2,84669.59999999999,0.308,179
9,2,77258.4,0.3247,173
我需要将字段[3]、[4]和[5]中的值,即'inc'、'a'和'b',转换为浮点数,然后追加到households以创建一个列表。
最后,它应该打印len(households)。
我正在尝试这样做:
import csv
import scipy.optimize as opt
def read_households(filename):
households=[]
fh = open("households.csv", 'r')
reader = csv.DictReader(fh) #创建一个用于读取文件的'reader'对象
for hh in reader:
reader[3,4,5] = float(reader[3,4,5])
households.append(hh)
print("lines read:", len(households))
return(household)
它没有给我任何输出。
英文:
I have csv file that looks like this:
id,type,inc,a,b
1,2,63376.799999999996,0.3061,187
2,1,58087.700000000004,0.3361,178
3,1,52699.9,0.3482,181
4,2,72964.8,0.3186,177
5,4,111589.79999999999,0.268,154
6,4,107618.0,0.2583,150
7,2,87109.2,0.3233,193
8,2,84669.59999999999,0.308,179
9,2,77258.4,0.3247,173
I need to transform values from fields [3], [4] and [5] that are 'inc', 'a' and 'b' into float and than append to households to create a list.
Finally it should print len(households)
I am trying this
import csv
import scipy.optimize as opt
def read_households(filename):
households=[]
fh = open("households.csv", 'r')
reader = csv.DictReader(fh) #creates object 'reader' to read the file
for hh in reader:
reader[3,4,5] = float(reader[3,4,5])
households.append(hh)
print("lines read:", len(households))
return(household)
It does not give me anything.
答案1
得分: 0
尝试使用以下代码从CSV文件中获取一些输出:
import csv
import scipy.optimize as opt
def read_households(filename):
households = []
fh = open(filename, 'r')
reader = csv.DictReader(fh)
for hh in reader:
hh['inc'] = float(hh['inc'])
hh['a'] = float(hh['a'])
hh['b'] = float(hh['b'])
households.append(hh)
print("读取行数:", len(households))
return households
# 调用函数
read_households('csv文件的路径')
结果:
读取行数: 9
[{'id': '1', 'type': '2', 'inc': 63376.8, 'a': 0.3061, 'b': 187.0},
{'id': '2', 'type': '1', 'inc': 58087.7, 'a': 0.3361, 'b': 178.0},
{'id': '3', 'type': '1', 'inc': 52699.9, 'a': 0.3482, 'b': 181.0},
{'id': '4', 'type': '2', 'inc': 72964.8, 'a': 0.3186, 'b': 177.0},
{'id': '5', 'type': '4', 'inc': 111589.8, 'a': 0.268, 'b': 154.0},
{'id': '6', 'type': '4', 'inc': 107618.0, 'a': 0.2583, 'b': 150.0},
{'id': '7', 'type': '2', 'inc': 87109.2, 'a': 0.3233, 'b': 193.0},
{'id': '8', 'type': '2', 'inc': 84669.6, 'a': 0.308, 'b': 179.0},
{'id': '9', 'type': '2', 'inc': 77258.4, 'a': 0.3247, 'b': 173.0}]
英文:
Try this code for get some output from CSV.
import csv
import scipy.optimize as opt
def read_households(filename):
households=[]
fh = open(filename, 'r')
reader = csv.DictReader(fh)
for hh in reader:
hh['inc'] = float(hh['inc'])
hh['a'] = float(hh['a'])
hh['b'] = float(hh['b'])
households.append(hh)
print("lines read:", len(households))
return households
# call function
read_households('path_of_csv_file')
Result :
lines read: 9
[{'id': '1', 'type': '2', 'inc': 63376.799999999996, 'a': 0.3061, 'b': 187.0},
{'id': '2', 'type': '1', 'inc': 58087.700000000004, 'a': 0.3361, 'b': 178.0},
{'id': '3', 'type': '1', 'inc': 52699.9, 'a': 0.3482, 'b': 181.0},
{'id': '4', 'type': '2', 'inc': 72964.8, 'a': 0.3186, 'b': 177.0},
{'id': '5', 'type': '4', 'inc': 111589.79999999999, 'a': 0.268, 'b': 154.0},
{'id': '6', 'type': '4', 'inc': 107618.0, 'a': 0.2583, 'b': 150.0},
{'id': '7', 'type': '2', 'inc': 87109.2, 'a': 0.3233, 'b': 193.0},
{'id': '8', 'type': '2', 'inc': 84669.59999999999, 'a': 0.308, 'b': 179.0},
{'id': '9', 'type': '2', 'inc': 77258.4, 'a': 0.3247, 'b': 173.0}]
答案2
得分: 0
如果你能使用`pandas`,那么这是一个非常简单的方法:
```python
import pandas as pd
df = pd.read_csv('file.csv')
df = df.astype({'inc': 'float', 'a': 'float', 'b': 'float'})
print(df)
print(f"Lines read: {df.shape[0]}")
运行后得到:
id type inc a b
0 1 2 63376.8 0.3061 187.0
1 2 1 58087.7 0.3361 178.0
2 3 1 52699.9 0.3482 181.0
...
8 9 2 77258.4 0.3247 173.0
Lines read: 9
如果你需要将输出转换为字典列表:
households = df.to_dict('records')
这样就完成了
<details>
<summary>英文:</summary>
If you can use `pandas` then this is a very easy way:
import pandas as pd
df = pd.read_csv('file.csv')
df = df.astype({'inc': 'float', 'a': 'float', 'b': 'float'})
print(df)
print(f"Lines read: {df.shape[0]}")
When run gives:
id type inc a b
0 1 2 63376.8 0.3061 187.0
1 2 1 58087.7 0.3361 178.0
2 3 1 52699.9 0.3482 181.0
...
8 9 2 77258.4 0.3247 173.0
Lines read: 9
If you need the output in the list of dictionaries output:
households = df.to_dict('records')
and we are done
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论