简体   繁体   English

如何找到字符串的特定部分?

[英]How can i find a specific part of a string?

Im accessing a csv file, looping through all of its rows(strings) and i want too keep / print all parts of each string which start with a ".", has two words in the middle and ends with either a "."我正在访问一个 csv 文件,循环遍历它的所有行(字符串),我也想保留/打印每个字符串的所有以“。”开头的部分,中间有两个单词并以“。”结尾。 "?" “?” or ".".或者 ”。”。

For example, if the string was: "This is my new channel. Please subscribe."例如,如果字符串是:“这是我的新频道。请订阅。” i'd only want to keep the ". Please subscribe!"我只想保留“。请订阅!”

So far i only have this to show me how many words are inside each string:到目前为止,我只有这个来告诉我每个字符串中有多少个单词:

with open("data2.csv", encoding="utf-8", newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        rowstr = str(row[1])
        res = len(row[1].split())
        print(res)

I've tried:我试过了:

with open("data2.csv", encoding="utf-8", newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        rowstr = row[1]
        res = len(row[1].split())
        re.findall(r"\.\S+\s\S+[.?!]", rowstr)
        print(row[1])

I get no output from findall, only from printing row[1]我没有从 findall 得到 output,只能从打印行 [1]

Fixed it解决它

Working code:工作代码:

with open("data2.csv", encoding="utf-8", newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        rowstr = row[1]
        res = len(row[1].split())
        finalData = re.findall(r"(\.\W\w+\W\w+[\.\?!])", rowstr)
        print(finalData)

You can use regular expression:您可以使用正则表达式:

re.findall(r'(\.\W\w+\W\w+[\.\?!])$',"This is my new channel. Please subscribe!" )

which output: ['. Please subscribe!']其中output: ['. Please subscribe!'] ['. Please subscribe!']

Regex is the best solution to the problems like this.正则表达式是此类问题的最佳解决方案。 Please refer here here !请参考这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM