Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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_Python 3.x - Fatal编程技术网

Python 将函数应用于文件中每一行的特定表达式

Python 将函数应用于文件中每一行的特定表达式,python,python-3.x,Python,Python 3.x,我目前正在为每一个案例实例读取一个文件的内容,其中的行符合特定的标准。请参见下面的代码 from string import punctuation fpath = open('Redshift_twb_1.txt', 'r') lines = fpath.readlines() fpath_write = open('Redshift_1_new.txt', 'w+') # filter the list; with the string 'apple' # replace 'apple

我目前正在为每一个案例实例读取一个文件的内容,其中的行符合特定的标准。请参见下面的代码

from string import punctuation

fpath = open('Redshift_twb_1.txt', 'r')
lines = fpath.readlines()

fpath_write = open('Redshift_1_new.txt', 'w+')

# filter the list; with the string 'apple'
# replace 'apple' with whatever string you want to find
temp_out_lines = [line for line in lines if '<column caption' in line]
out_lines = [line for line in temp_out_lines if 'param-domain-type' not in line]

# Lambda function that maps .lower() function to every element of the list out_lines
lower_lines = map(lambda x:x.lower(), out_lines)

# Join the lines into a single string
output = '\n'.join(lower_lines)

# write it
fpath_write.write(output)

fpath.close()
fpath_write.close()
目标是在将每一行添加到新的txt文件之前检查每一行,对于
name='[*****]'
的每个实例,将
[]
中的值设置为小写。目前,它们是大写的

注意:只有参数
名称=
[]
中的值可以小写。行中还有其他参数必须保持大写

谢谢

编辑:另一个选项是执行makeshift查找并替换,该操作将查找具有
name='[ABC]'
的所有实例,并将其替换为
name='[ABC]'
。但是,我仍然不知道如何独自去做这件事

Edit2:在实现Regex时,我还使用for循环遍历txt文件的每个实例。。。请参阅下面的代码

for x in range(len(out_lines)):
    print(out_lines[x])
    test = str(out_lines[x])
    out_lines[x] = re.sub(r"(name='([.*?])')", lambda m: m.group(1).lower(), test)
    print(out_lines[x])
但是,当我这样做时,我仍然得到相同的输出:

<column caption='Location' datatype='string' name='[MANAGEMENT_LOCATION]' role='dimension' type='nominal' />

<column caption='Location' datatype='string' name='[MANAGEMENT_LOCATION]' role='dimension' type='nominal' />

您可以使用re python模块替换必要的子字符串

import re
re.sub(r"(name='(\[.*?\])')", lambda m: m.group(1).lower(), <YOUR TEXT>)
重新导入
re.sub(r“(name=”(\[.*?\])”,lambda m:m.group(1.lower(),)

如果我要对此使用正则表达式,那么如何维护文本的值,而只是将其小写?arg做什么@Nick Po是需要替换子字符串的文本
your_text=”“re.sub(r)(name=”([.*?])”,lambda m:m.group(1)。lower(),your_text)
输出结果将是您可以使用re.sub(r)(name=”([.*)”),lambda m:m.group(1)。capitale(),您的_text)进行大写@Chandler Cree感谢您的帮助,请查看问题的底部以获得后续信息@Nick Posorry在我的评论中我使用了错误的正则表达式。要以正确的方式捕获,您需要使用
out\u line[x]=re.sub(r)(name='(\[.\]]),lambda m:m.group(1.lower(),test)
此外,您还可以在一些web工具(如@ChandlerCree)中检查您的正则表达式模式。如果您需要提高参数值的能力,最好使用这种形式
re.sub(r)(name)='\[(?)\]'”,lambda m:f“{m.group(1)}='[{m.group(2).lower()}]',test)
这样我们必须捕获组:1捕获参数的名称,2捕获值,然后我们使用掩码{parametr}='[{value}]'来格式化。这样你就可以对值进行上下转换。@Chandler Cree
import re
re.sub(r"(name='(\[.*?\])')", lambda m: m.group(1).lower(), <YOUR TEXT>)