【发布时间】:2020-08-21 14:38:57
【问题描述】:
我正在尝试处理一个文件,我需要删除文件中的无关信息;值得注意的是,我正在尝试删除括号 [] 包括括号内和括号之间的文本 [] [] 块,说这些块之间的所有内容包括它们本身,但打印它之外的所有内容。
下面是我的带有数据样本的文本文件:
$ cat smb
Hi this is my config file.
Please dont delete it
[homes]
browseable = No
comment = Your Home
create mode = 0640
csc policy = disable
directory mask = 0750
public = No
writeable = Yes
[proj]
browseable = Yes
comment = Project directories
csc policy = disable
path = /proj
public = No
writeable = Yes
[]
This last second line.
End of the line.
期望的输出:
Hi this is my config file.
Please dont delete it
This last second line.
End of the line.
根据我的理解和重新搜索,我尝试了什么:
$ cat test.py
with open("smb", "r") as file:
for line in file:
start = line.find( '[' )
end = line.find( ']' )
if start != -1 and end != -1:
result = line[start+1:end]
print(result)
输出:
$ ./test.py
homes
proj
【问题讨论】:
标签: python python-3.x regex pandas python-3.6