我有一些C代碼工作得很好:內EMPLOYEE.DAT
Python的替代FSCANF C代碼
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp=fopen("EMPLOYEE.DAT","r");
if(fp==NULL)
{
puts("Cannot open file";
exit(1);
}
while(fscanf(f,"%s %d %f",&e.name,&e.age,&e.bs)!=EOF)
printf("%s %d %f\n",e.name,e.age,e.bs);
fclose(fp);
return 0;
}
數據:
Sunil 34 1250.50
Sameer 21 1300.50
rahul 34 1400.50
我有麻煩翻譯這段代碼的Python:
while(fscanf(f,"%s %d %f",&e.name,&e.age,&e.bs)!=EOF)
printf("%s %d %f\n",e.name,e.age,e.bs);
有什麼辦法可以在Python中實現嗎?此外,什麼是Pythonic替代品exit()
& EOF
?
[在Python的sscanf]的可能的複製(https://stackoverflow.com/questions/2175080/sscanf-in-python) – dvb