我有一个包含以下各列的CSV文件,
ZoneMaterialName1,ZoneThickness1
Copper,2.5
Copper,2.5
Aluminium,3
Zinc,
Zinc,
Zinc,6
Aluminium,4
可以看出,某些值重复多次,有时可能是空白或一个句点。
我想要一个仅具有唯一值的哈希表,例如
ZoneMaterialName1,ZoneThickness1
Copper:[2.5]
Aluminium:[3,4]
Zinc:[6]
这是我想出的代码,输出缺少2.5之类的浮点数,并且还允许空白和句点。
import csv
from collections import defaultdict
import csv
afile = open('/mnt/c/python_test/Book2.csv', 'r+')
csvReader1 = csv.reader(afile)
reader = csv.DictReader(open('/mnt/c/python_test/Book2.csv'))
nodes = defaultdict(type(''))
for row in reader:
if (row['ZoneThickness1'] !=' ' and row['ZoneThickness1'] !='.'):
nodes[row['ZoneMaterialName1']]+=(row['ZoneThickness1'])
new_dict = {a:list(set(b)) for a, b in nodes.items()}
print new_dict
方法:我最初是创建字典并将其值转换为集合的。
答案 0 :(得分:3)
我建议您尝试将第二列强制转换为@PostMapping("/technologyList")
public String uploadMultipartFile(@RequestParam("logo") MultipartFile file, @RequestParam("techName")String techName) {
User user = userService.findByUsername("wmangram");
try {
// save file to MySQL
Skills newSkill = new Skills(techName, file.getBytes(), user);
skillsService.createTechnology(newSkill);
return "File uploaded successfully! -> filename = " + file.getOriginalFilename();
} catch (Exception e) {
return "FAIL! Maybe You had uploaded the file before or the file's size > 500KB";
}
}
并仅添加那些是有效浮点数的值。
另外,您可以使用float
来避免某些材料的重复值。
这可以像这样完成(我使用set
,因为您为两个python版本都标记了这个问题):
Python 3.x
这将提供以下输出:
import collections
import csv
result = collections.defaultdict(set)
with open('test.txt', 'r') as f:
csv_r = csv.DictReader(f)
for row in csv_r:
try:
v = float(row['ZoneThickness1'])
except ValueError:
# skip this line, because it is not a valid float
continue
# this will add the material if it doesn't exist yet and
# will also add the value if it doesn't exist yet for this material
result[row['ZoneMaterialName1']].add(v)
for k, v in result.items():
print(k, v)