我目前在一个文本文件中看到了一系列的变化:
2 /vol/vol0/home /vol/vol0/home CifsPerm: 0, CifsType: 0, Remark:
Up-level share detected. 0 NFS /vol/vol0/home ntap
但需要一条线,像这样:
2 /vol/vol0/home /vol/vol0/home CifsPerm: 0, CifsType: 0, Remark: Up-level share detected. 0 NFS /vol/vol0/home ntap
出于某种原因,所有突然出现的新行都以“Up level”开头。我该怎么做呢?我对Python非常陌生,如果有任何关于如何解决此问题的想法,我将不胜感激。你知道吗
我的代码(出于某种原因,它将所有内容都放在同一行中):
def bad_line_2(line):
if "Up-level" in line:
return True
else:
return False
def inventory_append(in_file_2, out_fixed_inv):
try:
in_fp_2 = open(in_file_2, "r")
except IOError as e:
print("error opening {} for reading: {}".format(in_file_2, str(e)))
else:
try:
out_inv = open(out_fixed_inv, "w")
except IOError as e:
print("error opening {} for writing: {}".format(out_fixed_inv, str(e)))
else:
with open(in_file_2) as f:
out = integer.join(line.rstrip('\n') for line in f
out_inv.write(out)
def main():
in_file_2 = "Inventory 2017-12-21.txt"
out_fixed_inv = "fixed_inv.txt"
inventory_append(in_file_2, out_fixed_inv)
if __name__ == "__main__":
main()
文本文件示例:
2 usfptotnap101a C$ \vol\vol0 232 8 0 4474560 Share 3 0 CifsPerm: 0, CifsType: 2147483648, Remark: Remote Administration 0 CIFS & NFS /vol/vol0 ntap
2 usfptotnap101a ETC$ \vol\vol0\etc 508 1 0 4474561 Share 1 1 CifsPerm: 0, CifsType: 2147483648, Remark: Remote Administration
Up-level share detected. 0 CIFS NULL ntap
2 usfptotnap101a Varonis$ \vol\it_tot101a_181099\Varonis 7159534 44 4 4474551 Share 1 1 CifsPerm: 0, CifsType: 0, Remark:
Up-level share detected. 0 CIFS NULL ntap
2 usfptotnap101a smtest \vol\smtest\smtree 7715986 1 0 4474559 Share 1 0 CifsPerm: 0, CifsType: 0, Remark: 0 CIFS NULL ntap
既然你说的是这个例子的变体,那么这里有一些东西可能会对你有所帮助:
现在,这是干什么的?它将第1行和第2行、第3行和第4行等配对,然后将它们串联起来。这是一个很好的解决方案,如果你的文件不是很大的大小。如果是,还有其他解决办法。对于大文件来说,它不是一个好的解决方案的原因是它用
readlines
一次读取所有行。你知道吗编辑1
上面的代码似乎不合适,因为行不是按假定的模式分割的。你知道吗
以您使用的文件和函数为例,更新后的版本如下所示:
相关问题 更多 >
编程相关推荐