【问题标题】:How do I scan a sentence in Python [duplicate]如何在Python中扫描一个句子[重复]
【发布时间】:2018-07-07 03:52:37
【问题描述】:

假设我有一个文本文件:

作为一名经理,他告诉 FIFA TV,他以一种谨慎的方式传达他的信息。 “我不是小伙子中的一员,”索斯盖特解释道。

有没有办法在引号 (") 中获取句子并将该句子保存为变量?我知道我必须使用扫描仪方法,但我是 python 语言的新手,我不知道如何。谁能给我一个如何存储这个的例子?

【问题讨论】:

  • " 中的句子有特殊字符,因此不能将其用作变量名。您需要添加_ 之类的字符来填补空白,然后您的任务就可以完成了。也检查一下stackoverflow.com/questions/5036700/…
  • 我不希望使用该主题中讨论的诸如 re 之类的模块,这只是我的看法,但我认为它们会使事情复杂化。
  • 我的疑问是,您想使用" 中的字符串作为变量名(动态创建变量)还是只想将其分配给其他变量?
  • 我只想获取 " 中的字符串并用另一种方法对其进行转换@RishikeshAgrawani

标签: python string nltk


【解决方案1】:

如果您可以确定要解析的字符串中始终存在两个双引号,则可以简单地使用 str.split('"')[0] 提取它们之间的内容。

>>> s = '''As a manager, he told FIFA TV he communicates his messages in a measured way. "I’m not one of the lads," Southgate explained.'''
>>> s.split('"')[1]
'I’m not one of the lads,'

编辑:我现在看到您的输入字符串实际上使用了倾斜的双引号 ,而不是标准的双引号 ",在这种情况下,我建议您改用以下内容:

s = '''As a manager, he told FIFA TV he communicates his messages in a measured way. “I’m not one of the lads,” Southgate explained.'''
print(s[s.find('“') + 1:s.find('”')])

这个输出:

I’m not one of the lads,

【讨论】:

  • 这只是将句子拆分为单词。我的意思是,把句子放在 "
  • 请仔细看。它正确地得到了引号之间的句子:I’m not one of the lads,。我刚刚编辑了我的答案以使其更易于阅读。
  • 即使我粘贴你的代码也不起作用。错误说 IndexError: list index out of range
  • 我现在看到这是因为您的输入字符串实际上使用了倾斜的双引号 ,而不是标准的双引号 "
  • 他们不一样吗?
【解决方案2】:

你可能想做这样的事情:

file = open("filename.txt","r") # Opens the file
sentence = file.readline().split() # ['A','s',' ','a',' ','m'...]
startQuote = sentence.index('"') # Finds first occurence
endQuote = sentence[startQuote::].index("'") # Finds first occurrence after first quote
stringSentence = ''.join(sentence[startQuote:endQuote:]) # Creates string with splicing

如果您希望代码适应“智能引号”,您可以将其简化为:

file = open("filename.txt","r") # Opens the file
sentence = file.readline().split() # ['A','s',' ','a',' ','m'...]
startQuote = sentence.index(“) # Finds first occurence
endQuote = sentence.index(”) # Finds first occurrence of end quote
stringSentence = ''.join(sentence[startQuote:endQuote:]) # Creates string with splicing

您可能需要修复此代码中的一些“围栏后错误”,因为我尚未对其进行测试。

我希望这能让你知道你需要做什么。

【讨论】:

  • 这个给我错误。错误说 startQuote = sentence.index("\"") # 发现第一次出现 ValueError: '"' is not in list
  • 这可能是因为您在示例中使用的引号不是引号。它们是“聪明的引语”,如果你仔细看,你会明白我的意思。它们略微倾斜。
【解决方案3】:

假设您在名为 sentence.txt 的文件中有一行文本。

sentence.txt

As a manager, he told FIFA TV he communicates his messages in a measured way. "I'm not one of the lads," Southgate explained.

现在,您可以尝试下面的代码来读取上面的行并提取包含在"(双引号)中的子字符串。

# -*- coding: utf-8 -*-
with open('sentence.txt', encoding='utf-8') as f:
    sentence = f.read().strip();

words = sentence.split('\"');

if len(words) == 3:
    string_in_double_quote = words[1];
    print string_in_double_quote # I'm not one of the lads,
else:
    print 'WARNING: String in text file does not have 2 double quotes, make sure to have it'

【讨论】:

  • 这是可行的,但即使我复制粘贴文本和代码,输出也是警告
  • 粘贴你的字符串,我会检查并更新它。让我看看是什么问题。
  • 这是我的 Windows CMD 中的内容,e:\Users\Rishikesh\Python3\Practice\DoubleQuoteStringSeparation>python main.py I'm not one of the lads,
  • 老兄,我只是粘贴上面的文字。我正在使用 py 2.7
  • 实际上你复制了旧代码(我忘了删除[1])。现在请尝试当前代码。
猜你喜欢
  • 2013-09-04
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 2019-10-13
  • 2017-03-30
  • 1970-01-01
  • 2014-11-22
相关资源
最近更新 更多