Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 删除前缀和后缀,同时保留python_Python 3.x_Sorting_Data Cleaning - Fatal编程技术网

Python 3.x 删除前缀和后缀,同时保留python

Python 3.x 删除前缀和后缀,同时保留python,python-3.x,sorting,data-cleaning,Python 3.x,Sorting,Data Cleaning,你好,我想清理我的数据文件 并希望: 删除前缀为“[”或后缀为“]”的单词,但保留这些单词 三个符号:[/]、[/]和[*] 我的示例文本与我阅读的文件相同: 有一个男孩在罐子里放了一只青蛙。第二天早上,男孩和狗在睡觉。第二天早上,男孩和狗在睡觉他们开始到处寻找它。\n',“\t[/-]它不在他的房间里。\n',“\t他在外面叫它来。\n',“\t狗失去了抓窗户和窗台的能力,掉了下来,\n'] 我尝试过使用startswith和endswith,但我似乎无法保留符号 clean=[“”.join

你好,我想清理我的数据文件 并希望: 删除前缀为“[”或后缀为“]”的单词,但保留这些单词 三个符号:[/]、[/]和[*]

我的示例文本与我阅读的文件相同:

有一个男孩在罐子里放了一只青蛙。第二天早上,男孩和狗在睡觉。第二天早上,男孩和狗在睡觉他们开始到处寻找它。\n',“\t[/-]它不在他的房间里。\n',“\t他在外面叫它来。\n',“\t狗失去了抓窗户和窗台的能力,掉了下来,\n']

我尝试过使用startswith和endswith,但我似乎无法保留符号

clean=[“”.join(y表示x.split(“”)中的y,如果不是y.startswith(前缀)或y.endswith(后缀))不是“[/]”、“[/]”和“[*]”表示文本中的x]

(很抱歉,我的代码很糟糕,试图整理我的想法)

编辑以保持输出行格式

Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.19.0 -- An enhanced Interactive Python.

In [1]: def clean(txt):
   ...:     return [' '.join(w for w in line.split(' ') 
   ...:                      if not (w[0] == '[' or w[-1] == ']')
   ...:                         or w in {'[/]', '[//]', '[*]'}) 
   ...:             for line in txt]

In [2]: txt = ['\tthere is a boy that had a frog in a jar .\n',
   ...:  '\tand a d(og) [/] dog is looking in jar .\n',
   ...:  '\tyeah .\n',
   ...:  '\tthen the windows opened .\n',
   ...:  "\tand it's at night .\n",
   ...:  '\tand through the night , while the boy was asleep and the dog\n',
   ...:  '\tthe next morning the [/] the boy and the dog was [*] wondering\n',
   ...:  '\tso they started looking all over for it .\n',
   ...:  '\t [/-] it was not in his room .\n',
   ...:  '\tso he called outside for it .\n',
   ...:  '\tand the dog loses his grip on the window+sill and falls off and\n']

In [3]: clean(txt)
Out[3]: 
['\tthere is a boy that had a frog in a jar .\n',
 '\tand a d(og) [/] dog is looking in jar .\n',
 '\tyeah .\n',
 '\tthen the windows opened .\n',
 "\tand it's at night .\n",
 '\tand through the night , while the boy was asleep and the dog\n',
 '\tthe next morning the [/] the boy and the dog was [*] wondering\n',
 '\tso they started looking all over for it .\n',
 '\t it was not in his room .\n',
 '\tso he called outside for it .\n',
 '\tand the dog loses his grip on the window+sill and falls off and\n']

In [4]: 

请发布一些您尝试过的代码以及预期输出和实际输出。但是,请参见Python3.9,我认为这可能是一个X/Y问题。你能展示一下你目前正在使用的代码吗,这样我们就能更好地帮助你了?只是添加了我的代码,不工作,但在那里练习我的方法taken@jixubi请添加所有相关代码。比如,你如何得到你在列表理解中循环的
text