Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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删除某些特定文本_Python_String_Replace - Fatal编程技术网

在同一文本文件的不同位置重新排列文本,并对其进行一些更改,然后使用python删除某些特定文本

在同一文本文件的不同位置重新排列文本,并对其进行一些更改,然后使用python删除某些特定文本,python,string,replace,Python,String,Replace,有一些输入 1. "Want to learn more? (link to https://www.google.com) Click here." 2 "Want to learn more? (link to https://https://www.google.com) website." 预期产出分别为: 1 "Want to learn more? [Click here] (https://www.google.com)." 2 "Want to learn more? [

有一些输入

1. "Want to learn more? (link to https://www.google.com) Click here."

2  "Want to learn more? (link to https://https://www.google.com) website."
预期产出分别为:

1 "Want to learn more? [Click here] (https://www.google.com)."

2 "Want to learn more? [website] (https://https://www.google.com)."
说明:


我想删除URL和文本中的
链接,在
()
重新排列
()
之前,使用
[]
插入
而不使用
regex
您可以使用
text.split()
拆分为以后可以重新设置范围的部分。I代码I将这些部分显示为
a、b、c、d

text = '''
1. "Want to learn more? (link to https://www.google.com) Click here."
2  "Want to learn more? (link to https://https://www.google.com) website."
'''

for line in text.splitlines():
    if line:
        #print(line)
        a, b = line.split('(link to ')
        b, c = b.split(') ')
        c, d = c.split('.')
        print(' a:', a)
        print(' b:', b)
        print(' c:', c)
        print(' d:', d)
        print('{}[{}] ({}).{}'.format(a, c, b, d))
结果:

 a: 1. "Want to learn more? 
 b: https://www.google.com
 c: Click here
 d: "
1. "Want to learn more? [Click here] (https://www.google.com)."
 a: 2  "Want to learn more? 
 b: https://https://www.google.com
 c: website
 d: "
2  "Want to learn more? [website] (https://https://www.google.com)."

re.split()相同


你试了什么?显示您的代码和完整的错误消息?我会尝试
text.find()
text.split()
(链接到
以获得3个元素-在
之前的文本(链接到
,在
之间的文本(链接到
,然后创建具有预期输出的新文本没有问题。或者您可以使用
regex
进行此操作。将open(r'C:\Users\test.txt')作为infle导入re,将open(r'C:\test1.txt',w')作为outfile:copy=False作为infle中的行:if line.strip()=“[”:copy=True如果copy:#翻转以包括end,正如Dan H指出的那样outfile.write(line)if line.strip()=“]”:copy=false始终添加有问题的代码、数据和错误消息,而不是在注释中添加。这样会更具可读性。
text = '''
1. "Want to learn more? (link to https://www.google.com) Click here."
2  "Want to learn more? (link to https://https://www.google.com) website."
'''

import re

for line in text.splitlines():
    if line:
        a = re.split('(.*)\(link to (.*)\) (.*)(\.")', line)
        print(a)
        print('{}[{}] ({}){}'.format(a[1], a[3], a[2], a[4]))