I am trying to know how to assign certain variables from the text files per line in Python.
Text file:
0 Longoria Julia Manager
1 Valdivia Corina Surgeon
In the C++ version, the code to assign each variable per line is coded like this:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int ID[2];
string lName[2];
string fName[2];
string jobTitle[2];
int main()
{
fstream file;
file.open("Employee List.txt");
index = 0;
while (!file.eof())
{
file >> ID[index] >> lName[index] >> fName[index] >> jobTitle[index];
index++;
}
file.close();
return 0;
}
In the Java version, the code to assign each variable per line is coded like this:
import java.io.*;
import java.util.*;
public class fileToVariable
{
public static void main(String[] args)
{
int ID[2] = {0, 0};
String lName[2] = {"", ""};
String fName[2] = {"", ""};
String jobTitle[2] = {"", ""};
try
{
ifstream = new Scanner(new fileInputStream("Employee List.txt"));
}
catch (FileNotFoundException e)
{
}
while (ifstream.hasNextLine())
{
ID[index] = ifstream.nextInt();
lName[index] = ifstream.next();
fName[index] = ifstream.next();
jobTitle[index] = ifstream.next();
index++;
}
}
Does anyone know how the Python equivalent to assigning each variables from the file is coded?