Python 如何仅获取特定字符后面的字符串?

Python 如何仅获取特定字符后面的字符串?,python,Python,如果我有这个密码 import csv txt_file = r"input.txt" csv_file = r"Good_data.csv" in_txt = csv.reader(open(txt_file, "r"), delimiter = ',') out_csv = csv.writer(open(csv_file, 'w')) out_csv.writerows(in_txt) 这段代码将字符串从txt移动到excel,但我只希望移动字母Y之后的字符串 例如,这是input.tx

如果我有这个密码

import csv
txt_file = r"input.txt"
csv_file = r"Good_data.csv"
in_txt = csv.reader(open(txt_file, "r"), delimiter = ',')
out_csv = csv.writer(open(csv_file, 'w'))
out_csv.writerows(in_txt)
这段代码将字符串从txt移动到excel,但我只希望移动字母Y之后的字符串 例如,这是input.txt中的一行

u'gY': -0.009,Y'gm': 0.0011, Y'ay'

TQ

您可以使用
正向查找(?您希望以什么格式排列数据?看起来不凌乱,看起来像JSON,不确定它是否格式错误,或者您只是像那样发布它看起来您有字典,您可以使用pandas来获取数据帧…这是文档我很抱歉我没有指定如何排列它…我想通过只打印后面的单词来排列它r字母w字母
Y
?显示了
input.txt
的示例,使我们能够理解您的问题。
import re

pattern = r"(?<=Y)'(\w.+?)'"

string="u'gY': -0.009,Y'gm': 0.0011, Y'ay'"

print(re.findall(pattern,string))
['gm', 'ay']
Positive Lookbehind (?<=Y)
Assert that the Regex below matches
**Y** matches the character Y literally (case sensitive)
**'** matches the character ' literally (case sensitive)
1st Capturing Group **(\w.+?)**
**\w** matches any word character (equal to [a-zA-Z0-9_])
**.+?** matches any character (except for line terminators)
**+?** Quantifier — Matches between one and unlimited times, as few times as possible, expanding as needed (lazy)
' matches the character ' literally (case sensitive)