This is about reading word by word from a text file during programming using Python. The method works in Python versions prior to Python 3. This article will stick to Python version 3.4 for the purpose of demonstrations.
Often times, it is required to work with ASCII based or text based input files for storing values to variables. Text based input files are preferred over the interactive input type for a variety of reasons. Some among them are,
1. Do not require to input or enter the values each time the code runs while testing or developmental stage.
2. Always easy to trace back in case of errors or unexpected results.
3. Easy to maintain a well structured input file for record and documentation.
Back to the topic, in order to read the values word by word, follow the steps below.
1. The words must be separated using a delimiter (space, comma, semicolon ...).
2. Read a full line at once [readline()].
3. Slice each line character by character.
4. Append each characters until the delimiter character is encountered to an empty variable (field) of string data type.
5. Append the qualifying field and initialize the field variable to empty string. This is important.
6. The list is now ready with values word by word.
Example:
TEST.INP is the input file. It contains the following lines.
First Name is Google
Second Name is Google2
Steps as in Python code..
input_file = open("TEST.INP", 'r') # Opening the input file with read permission or rights [instancing]
all_lines = input_file.readlines() # All the lines are read at once and stored to all_lines
input_file.close() # Close the file and retrieve the variable from sytem memory
print(len(all_lines), "Total lines.") #Prints to check the total number of lines in the file
all_fields=[] #Initialise the all_fields list
for i in range(0,len(all_lines),1): # Start the main loop to iterate over all the lines
field="" #field, the empty string variable is initialized. Indentation is important in python
line_i = all_lines[i] # One line from the file is taken to variable line_i to perform slicing operation
for j in range(0,len(line_i),1): #Analyzing the line, character by character
if line_i[j] != ";": #Checking for the separator [!= is not equal to]
field+=line_i[j] #Concatenating the field character by character
elif line_i[j]==";": # Condition to perform if the character is the separator
if field != "": #when the separator is met, do the following line
all_fields.append(field) # Appending to the main list
if field != "":
all_fields.append(field)
Now the all_fields list will have the words, word by word.
See the snapshot from the Python screen.
5. Append the qualifying field and initialize the field variable to empty string. This is important.
6. The list is now ready with values word by word.
Example:
TEST.INP is the input file. It contains the following lines.
First Name is Google
Second Name is Google2
Steps as in Python code..
input_file = open("TEST.INP", 'r') # Opening the input file with read permission or rights [instancing]
all_lines = input_file.readlines() # All the lines are read at once and stored to all_lines
input_file.close() # Close the file and retrieve the variable from sytem memory
print(len(all_lines), "Total lines.") #Prints to check the total number of lines in the file
all_fields=[] #Initialise the all_fields list
for i in range(0,len(all_lines),1): # Start the main loop to iterate over all the lines
field="" #field, the empty string variable is initialized. Indentation is important in python
line_i = all_lines[i] # One line from the file is taken to variable line_i to perform slicing operation
for j in range(0,len(line_i),1): #Analyzing the line, character by character
if line_i[j] != ";": #Checking for the separator [!= is not equal to]
field+=line_i[j] #Concatenating the field character by character
elif line_i[j]==";": # Condition to perform if the character is the separator
if field != "": #when the separator is met, do the following line
all_fields.append(field) # Appending to the main list
if field != "":
all_fields.append(field)
Now the all_fields list will have the words, word by word.
See the snapshot from the Python screen.
Comments
Post a Comment