我正在尝试编写一段简单的代码来读取CSV文件中的值,最多包含100个条目到一个结构数组中.
CSV文件的一行示例:
1,先生,詹姆斯,奎格利,主任,200000,0
我使用以下代码读取值,但是当我打印出值时,它们是不正确的
for(i = 0; i < 3; i++) /*just assuming number of entries here to demonstrate problem*/
{
fscanf(f, "%d,%s,%s,%s,%s,%d,%d", &inArray[i].ID, inArray[i].salutation, inArray[i].firstName, inArray[i].surName, inArray[i].position, &inArray[i].sal, &inArray[i].deleted);
}
Run Code Online (Sandbox Code Playgroud)
然后,当我打印出第一个名称时,所有值都分配给第一个名称:
for(j = 0; j < 3; j++) /* test by printing values*/
{
printf("Employee name is %s\n", inArray[j].firstName);
}
Run Code Online (Sandbox Code Playgroud)
ames,Quigley,Director,200000,0以这种方式给予等等.我确信这是我如何格式化fscanf线,但我不能让它工作.
这是我正在阅读的结构:
typedef struct Employee
{
int ID;
char salutation[4];
char firstName[21];
char surName[31];
char position[16];
int sal;
int deleted;
} Employee;
Run Code Online (Sandbox Code Playgroud) 我知道 PMD 可以在命令行上执行并在其中指定输出文件,但由于某些原因,只需将 Eclipse 插件结果保存到文件中就会更轻松。
有什么办法可以做到这一点吗?我已经四处寻找这个问题但还没有找到解决方案。
我见过类似的问题和答案,但没有一个能解决我的问题.
我希望我的视图执行用户组检查,然后通过变量将其传递给模板.然后,模板将使用该模板以不同的方式显示不同的用户组.
我的views.py:
def cans(request):
is_canner = request.user.groups.filter(name='canner') #check if user group = canner
can_list = Can.objects.order_by('name')
context = {'can_list': can_list}
return render(request, 'cans/cans.html', context) #need to return is_canner variable here
Run Code Online (Sandbox Code Playgroud)
在我的模板中,我会像这样使用变量:
{% if is_canner %} canner stuff goes here {% endif %}
Run Code Online (Sandbox Code Playgroud)
我不确定如何传递这个变量,我认为它使用上下文来发送它像这样:
return render(request, 'cans/cans.html', context({"is_canner": is_canner}))
Run Code Online (Sandbox Code Playgroud)
但这给了我错误 - 上下文不可调用.