'if statement' 删除所有不符合条件的行,我不想要那个
'if statement' removes all lines that don't match the condition, I dont want that
我是 Python 的新手,我正在为一些相当基本的事情而苦苦挣扎。
我想做什么:
我有一个包含多行的文本文件。我希望索引号 [-3] 乘以 2.
但是,我只想Python乘以每行的索引[-3] 如果索引号[6]等于1.
如果我的行的索引号 [6] 是 0,我不想将该数字乘以 2。我只希望该行保留在文本文件中,完全不变。
问题是:
如果我使用 'if' 语句,它会完美地乘以索引号 [-3] 如果我在行中。第 6 个索引为 1 的乘线示例,因此允许 Python 乘以 2:
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.1 0 flbo ||这变成 -->
FLBO id 'Boundary' st 0 ty 1 q_dw 0 0.2 0 flbo
但是,如果 Python 发现索引号 [6] 不是 1,它就会跳过该行。这不是我想要的,因为这样我就丢失了数据。在开始我的脚本之前,我的文本文件有 10 行。生成的文件有 9 行。所以它失去了一条线。例子":
FLBO id 'Boundary' st 0 ty 0 q_ dw 0 8 0 flbo ||这变成 -->
没什么。线不见了。
我的问题:
如何获取 Python 到 'remember' if 语句为假的行?
我试过使用pass函数,结果完全一样
我当前的脚本/解决方案:
我用一种非常丑陋的方式解决了这个问题,通过将索引号 [6] = 0 的行乘以 *1。这样数字就不会改变。但这不是一种干净的编程方式,我想提高效率。代码:
all_data = []
with open('C:\directory1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
if each_line_data[6] == '0':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 1
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
print(new_each_line_data)
with open('C:\directory2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
解决办法在评论里。这是一个缩进问题。
有效代码:
all_data = []
with open('C:\file1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
print(new_each_line_data)
with open('C:\file2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
您需要查看每一行的作用并取消缩进需要对每一行执行的操作:
all_data = []
with open('C:\directory1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
print(new_each_line_data)
with open('C:\directory2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
如您所见,加入行并追加现在发生在 =='1'
if 之外。这正是您需要对每一行执行的操作,无论它是 1 还是 0 或其他任何内容。 :)
您可以这样减少代码:
all_data = []
with open('C:\directory1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
multiplier = 2
elif each_line_data[6] == '0'
multiplier = 1
else:
# I'm not sure what you wish to do in this case
multiplier = 1
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
with open('C:\directory2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
我建议你使用标准库中的 csv
模块。
您还需要确保始终 record/output 该行,无论条件如何。正如@nathan 所建议的,有条件地选择乘数,然后无条件地处理行 是有意义的。
下面的解决方案是 streaming,因为它不会在内存中累积所有数据。相反,它处理每一行并将其写入,因此内存使用量是恒定的(而不是 O(N))。
import csv
import io
input_contents = """\
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.1 0 flbo
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.2 0 flbo
FLBO id 'Boundary' st 0 ty 0 q_ dw 0 8.0 0 flbo
"""
with io.StringIO(input_contents) as input_file, io.StringIO() as output_file:
reader = csv.reader(input_file, delimiter=" ")
writer = csv.writer(output_file, delimiter=" ")
for row in reader:
factor = 2.0 if row[6] == '1' else 1.0
row[-3] = float(row[-3]) * factor
writer.writerow(row)
# Show output for SO purposes
print(output_file.getvalue())
输出是这样的:
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.2 0 flbo
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.4 0 flbo
FLBO id 'Boundary' st 0 ty 0 q_ dw 0 8.0 0 flbo
我是 Python 的新手,我正在为一些相当基本的事情而苦苦挣扎。
我想做什么:
我有一个包含多行的文本文件。我希望索引号 [-3] 乘以 2.
但是,我只想Python乘以每行的索引[-3] 如果索引号[6]等于1.
如果我的行的索引号 [6] 是 0,我不想将该数字乘以 2。我只希望该行保留在文本文件中,完全不变。
问题是:
如果我使用 'if' 语句,它会完美地乘以索引号 [-3] 如果我在行中。第 6 个索引为 1 的乘线示例,因此允许 Python 乘以 2:
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.1 0 flbo ||这变成 -->
FLBO id 'Boundary' st 0 ty 1 q_dw 0 0.2 0 flbo
但是,如果 Python 发现索引号 [6] 不是 1,它就会跳过该行。这不是我想要的,因为这样我就丢失了数据。在开始我的脚本之前,我的文本文件有 10 行。生成的文件有 9 行。所以它失去了一条线。例子":
FLBO id 'Boundary' st 0 ty 0 q_ dw 0 8 0 flbo ||这变成 -->
没什么。线不见了。
我的问题:
如何获取 Python 到 'remember' if 语句为假的行?
我试过使用pass函数,结果完全一样
我当前的脚本/解决方案:
我用一种非常丑陋的方式解决了这个问题,通过将索引号 [6] = 0 的行乘以 *1。这样数字就不会改变。但这不是一种干净的编程方式,我想提高效率。代码:
all_data = []
with open('C:\directory1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
if each_line_data[6] == '0':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 1
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
print(new_each_line_data)
with open('C:\directory2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
解决办法在评论里。这是一个缩进问题。 有效代码:
all_data = []
with open('C:\file1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
print(new_each_line_data)
with open('C:\file2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
您需要查看每一行的作用并取消缩进需要对每一行执行的操作:
all_data = []
with open('C:\directory1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
print(new_each_line_data)
with open('C:\directory2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
如您所见,加入行并追加现在发生在 =='1'
if 之外。这正是您需要对每一行执行的操作,无论它是 1 还是 0 或其他任何内容。 :)
您可以这样减少代码:
all_data = []
with open('C:\directory1', 'r') as file_handler:
for line in file_handler.readlines():
if line.strip():
each_line_data = line.split()
if each_line_data[6] == '1':
multiplier = 2
elif each_line_data[6] == '0'
multiplier = 1
else:
# I'm not sure what you wish to do in this case
multiplier = 1
old_debiet = each_line_data[-3]
new_debiet = float(old_debiet) * 2
each_line_data[-3] = str(new_debiet)
new_each_line_data = ' '.join(each_line_data)
all_data.append(new_each_line_data)
with open('C:\directory2', 'w') as file_handler:
for item in all_data:
file_handler.write("{}\n".format(item))
我建议你使用标准库中的 csv
模块。
您还需要确保始终 record/output 该行,无论条件如何。正如@nathan 所建议的,有条件地选择乘数,然后无条件地处理行 是有意义的。
下面的解决方案是 streaming,因为它不会在内存中累积所有数据。相反,它处理每一行并将其写入,因此内存使用量是恒定的(而不是 O(N))。
import csv
import io
input_contents = """\
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.1 0 flbo
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.2 0 flbo
FLBO id 'Boundary' st 0 ty 0 q_ dw 0 8.0 0 flbo
"""
with io.StringIO(input_contents) as input_file, io.StringIO() as output_file:
reader = csv.reader(input_file, delimiter=" ")
writer = csv.writer(output_file, delimiter=" ")
for row in reader:
factor = 2.0 if row[6] == '1' else 1.0
row[-3] = float(row[-3]) * factor
writer.writerow(row)
# Show output for SO purposes
print(output_file.getvalue())
输出是这样的:
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.2 0 flbo
FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.4 0 flbo
FLBO id 'Boundary' st 0 ty 0 q_ dw 0 8.0 0 flbo