我在这里和那里混淆了几句话,我的不好。该示例应该使得打算比此时更清楚。
实施例: 我的.csv文件名为" file.csv":
Error: Could not find or load main class Client
当你像我这样调用我的函数时:
$ProcessList = gwmi Win32_PerfFormattedData_PerfProc_Process -ComputerName $ComputerName -Credential $cred | select IDProcess,Name,PercentProcessorTime | where { $_.Name -ne "_Total" -and $_.Name -ne "Idle"} | sort PercentProcessorTime -Descending | select -First 3
$TopProcess = @()
ForEach ($Process in $ProcessList) {
$row = new-object PSObject -Property @{
Id = $Process.IDProcess
Name = $Process.Name
User = (gwmi Win32_Process -ComputerName $ComputerName -Credential $cred | where {$_.ProcessId -eq $Process.IDProcess}).GetOwner().User
CPU = $Process.PercentProcessorTime
Description = (Get-Process -Id $Process.IDProcess).Description
}
$TopProcess += $row
}
您可以像这样调用您要求的值:
T2Y;Brad;0001
K5;Jan;0002
T21;En;3838
T22;aps;3804
T11;Jyke;3957
T08;John;3825
K05;Tim;0001
我似乎正常做的是通过读取.csv文件创建第一个字典,但我无法创建可以按照我的示例显示的方式调用的嵌套字典。我试着在下面的漫步中更深入一些。
以下是我当前代码如何吐出信息,可能是冗余信息
我现在的代码有点混乱,因为我试图将其形成一个嵌套的字典,但此时正在做的是它打开并读取.csv文件,它分裂了行";"并使用名称作为键来生成可调用的每一行的字典。然而,当前的障碍是,我只能使用单个键(名称)进行调用,并且它将整行返回为字典或查看该部分的内容。像这样:
>>> dict = function("file.csv")
如果我尝试在名称密钥后面添加另一个密钥,则代码会在错误代码中停止:
>>> dict['Brad']['code']
'T2Y'
>>> dict['John']['name']
'John'
答案 0 :(得分:0)
如上所述,这是一个非常简单的问题:您希望使用CSV文件中的信息创建嵌套字典。由于您没有指定第3列是什么,我只需将其标记为<svg width="390" height="248" viewBox="-266600, -68800, 195000, 124000" version="1.1" xmlns="http://www.w3.org/2000/svg">
<path id="closedPath" fill="#ff9966" d="M-250179-46928l-5051 1351l-867-1760l-33-146l-12-99l-82-678l-17-249l86-644l305-1800l158-2882l75-1425l-47-280l-22-131l-137-411l-300-892l1273 620l931-109l1957-734l1860-1096l292-192l884 547l2690 2153l480 963l36 244l-948 1878l-376 591l-60 567l-72 1147l97 847l-222 334l-122 117l-2403 2093l-353 76z"/>
<rect id="rectangle" fill="#66ff66" x="-126828" y="0" width="45000" height="45000"/>
</svg>
<script>
var rectangle = document.getElementById('rectangle');
var closedPath = document.getElementById('closedPath');
var svgRoot = closedPath.farthestViewportElement;
var rect = svgRoot.createSVGRect();
rect.x = rectangle.x.animVal.value;
rect.y = rectangle.y.animVal.value;
rect.height = rectangle.height.animVal.value;
rect.width = rectangle.width.animVal.value;
var hasIntersection = svgRoot.checkIntersection(closedPath, rect);
console.log(hasIntersection);
</script>
并使用字符串。我还假设该文件每个名称只包含一行:如果没有,将使用最新行。
id
这将打印:
import csv
user_info = {}
with open('data.txt') as csvfile:
data = csv.reader(csvfile, delimiter=';')
for code, name, id in data:
user_info[name] = { 'code': code, 'name': name, 'id': id }
print (user_info)