Python beginner here trying to learn how to make my code more streamlined. Sorry in advance for the long question, I don't know how much info you guys need in order to help me out.
I have a piece of code that does exactly what I need it to, but I feel like it should be more compact.
Basically my code takes a file, multiplies one number, then writes the file to a different directory every time it runs through the code.
To get the code to do what I want, I basically have to repeat the code 9x (once for every item in the multipliers list), because I want the result to write to a different directory every time.
Is there any way to compact this code? The code should take the 0th index and write to FileLocation1. Then take the 1st index and write to FileLocation2. It's probably a matter of using a for loop, but I don't know where to put it :(
The code is as follows:
#Identifying path where the file is that I want to multiply
path = 'C:\\DirectoryIWantToTakeFileFrom'
#Define the multipliers in the list below
multipliers = [0.01, 0.1, 0.25, 0.5, 1, 1.5, 1.7, 1.85, 2]
all_data0 = []
with open(path, 'r') as file_handler:
for multiplier in multipliers:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * multipliers[0]
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data0.append(new_each_line_data)
with open('C:\\WriteLocation1', 'w') as file_handler:
for item in all_data0:
file_handler.write("{}\n".format(item))
#Now I proceed to execute exactly the same code but I store the data in a different list (all_data1) and I change the writing directory
all_data1 = []
with open(path, 'r') as file_handler:
for multiplier in multipliers:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * multipliers[1]
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data1.append(new_each_line_data)
with open('C:\\WriteLocation2', 'w') as file_handler:
for item in all_data1:
file_handler.write("{}\n".format(item))
#And now I do this 7 more times, for WriteLocation3, 4, 5, 6, 7, 8 and 9.
One of the first things that we can do is extract the common code as a function. The arguments to the function would be the variables that change in every let's say - multiplication cycle. In your example, all the code gets repeated except for the lines new_debiet = float(old_debiet) * multipliers[0]
and with open('C:\\WriteLocation1', 'w') as file_handler:
. So these could be the arguments to the function.
Now let's define the function. The definition could look like:
def multiply_and_write(multiplier, file_to_write):
all_data0 = []
with open(path, 'r') as file_handler:
for multiplier in multipliers:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * multiplier
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data0.append(new_each_line_data)
with open(file_to_write, 'w') as file_handler:
for item in all_data0:
file_handler.write("{}\n".format(item))
Notice the change in the lines that are indicated above.
Now, we shall call this function for every multiplier and for every file location. Let's assume that we have the list of multipliers and file locations as:
multipliers = [0.01, 0.1, 0.25, 0.5, 1, 1.5, 1.7, 1.85, 2] # length of both lists is assumed to be the same
file_locations = ['C:\\WriteLocation1', 'C:\\WriteLocation2', ...]
Now, we iterate over these lists and call the function we defined earlier like:
for i in range(len(multipliers)):
multiply_and_write(multipliers[i], file_locations[i])
Hope this helps!
PS: There could be some more tweaks in making the code efficient. However, the point I wanted to convey is using a function will help reduce the code.