如何在写入不同目录的同时压缩这段代码?
How to condense this code while still writing to different directories?
Python初学者在这里尝试学习如何让我的代码更精简。很抱歉提前问了这么长的问题,我不知道你们需要多少信息才能帮助我。
我有一段代码完全符合我的需要,但我觉得它应该更紧凑。
基本上我的代码获取一个文件,乘以一个数字,然后在每次运行代码时将该文件写入不同的目录。
为了让代码执行我想要的操作,我基本上必须重复代码 9 次(乘数列表中的每个项目一次),因为我希望每次都将结果写入不同的目录。
有什么办法可以压缩这段代码吗?该代码应采用第 0 个索引并写入 FileLocation1。然后取第一个索引并写入 FileLocation2。这可能是使用for循环的问题,但我不知道把它放在哪里:(
代码如下:
#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.
将每个路径字符串放在一个列表中,然后循环列表:)
所以像这样:
list_of_write_locations = ['C:\WriteLocation1', 'C:\Writelocation2', ... ]
for location in list_of_write_locations:
with open(location, 'w') as file_handler:
我们可以做的第一件事就是将公共代码提取为函数。该函数的参数将是每次变化的变量 - 乘法循环。在您的示例中,除了行 new_debiet = float(old_debiet) * multipliers[0]
和 with open('C:\WriteLocation1', 'w') as file_handler:
之外,所有代码都会重复。所以这些可能是函数的参数。
现在让我们定义函数。定义可能如下所示:
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))
请注意上面指示的行的变化。
现在,我们将为每个乘数和每个文件位置调用此函数。假设我们有乘数列表和文件位置:
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', ...]
现在,我们遍历这些列表并调用我们之前定义的函数,如:
for i in range(len(multipliers)):
multiply_and_write(multipliers[i], file_locations[i])
希望对您有所帮助!
PS:在提高代码效率方面可能还有一些调整。然而,我想表达的一点是使用函数将有助于减少代码。
Python初学者在这里尝试学习如何让我的代码更精简。很抱歉提前问了这么长的问题,我不知道你们需要多少信息才能帮助我。
我有一段代码完全符合我的需要,但我觉得它应该更紧凑。
基本上我的代码获取一个文件,乘以一个数字,然后在每次运行代码时将该文件写入不同的目录。
为了让代码执行我想要的操作,我基本上必须重复代码 9 次(乘数列表中的每个项目一次),因为我希望每次都将结果写入不同的目录。
有什么办法可以压缩这段代码吗?该代码应采用第 0 个索引并写入 FileLocation1。然后取第一个索引并写入 FileLocation2。这可能是使用for循环的问题,但我不知道把它放在哪里:(
代码如下:
#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.
将每个路径字符串放在一个列表中,然后循环列表:)
所以像这样:
list_of_write_locations = ['C:\WriteLocation1', 'C:\Writelocation2', ... ]
for location in list_of_write_locations:
with open(location, 'w') as file_handler:
我们可以做的第一件事就是将公共代码提取为函数。该函数的参数将是每次变化的变量 - 乘法循环。在您的示例中,除了行 new_debiet = float(old_debiet) * multipliers[0]
和 with open('C:\WriteLocation1', 'w') as file_handler:
之外,所有代码都会重复。所以这些可能是函数的参数。
现在让我们定义函数。定义可能如下所示:
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))
请注意上面指示的行的变化。
现在,我们将为每个乘数和每个文件位置调用此函数。假设我们有乘数列表和文件位置:
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', ...]
现在,我们遍历这些列表并调用我们之前定义的函数,如:
for i in range(len(multipliers)):
multiply_and_write(multipliers[i], file_locations[i])
希望对您有所帮助!
PS:在提高代码效率方面可能还有一些调整。然而,我想表达的一点是使用函数将有助于减少代码。