【问题标题】:How to print different words in each line from the text document using python?如何使用python在文本文档的每一行中打印不同的单词?
【发布时间】:2020-09-23 00:57:42
【问题描述】:

我是 python 新手,我想从句子中打印不同的单词。下面是文本文件

Test.txt

1. As more than one social media historian has reminded few people, the Stonewall uprising was a rio-— more pointedly, a riot against police brutality, in response to an NYPD raid of Greenwich Village’s 
Stonewall Inn, in the early morning of June 29, 1969.
2. As more than one social media historian has reminded majority people, the Stonewall uprising was a riot — more pointedly, a riot against police brutality, in response to an NYPD raid of Greenwich Village’s Stonewall Inn, in the evening of July 12, 1979.

从上面的文本文件中,我想要从第一行开始的第 10 个单词(few)、第 11 个单词(people)、第 39 个单词(morning)、第 43 个单词(1969)。 第二行还有第 10 个词(多数)、第 11 个词(人)、第 39 个词(晚上)、第 43 个(1979 年)。

注意:在 Test.txt 中 1. 表示第一行,2. 表示第二行

为此,我尝试使用拆分功能。

with open('Test.txt', 'r') as file:
for line in file:
    print (line.split('reminded',1)[1])

这是我得到的输出

few people, the Stonewall uprising was a riot - more pointedly, a riot against police brutality, in response to an NYPD raid of Greenwich Villagers  Stonewall Inn, in the early morning of June 29, 1969.
majority people, the Stonewall uprising was a riot — more pointedly, a riot against police brutality, in response to an NYPD raid of Greenwich Village’s Stonewall Inn, in the evening of July 12, 1979.

如何在使用拆分功能后只打印一个单词?我很漂亮,我仍然需要改进这段代码。

任何帮助将不胜感激。 提前致谢。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    这是你的代码:

    
    datalist = ['few','people','morning','1969','majority','people','evening','1979']
    with open('file.txt', 'r') as file:
        data = file.read().replace('\n', ' ')
    
    outputlist = data.replace('.','').split(" ")
    for data in outputlist:
        if data in datalist:
            print(data)
    

    【讨论】:

    • 输出:文件“E:/python files/try.py”,第 13 行,在 outputlist = file.replace('.','').split('remitted', 1)[1].split(" ") AttributeError: '_io.TextIOWrapper' 对象没有属性 'replace'。尝试执行此代码时出现错误。是否有任何特定原因或诸如python版本之类的......
    • 我尝试了上面的代码,但它给我带来了错误。如果可能的话,你能否发布你得到的输出。
    • 几个早上 1969 年大多数晚上 19​​79 - 这是我的输出
    • 使用评分表示感谢
    【解决方案2】:

    使用 split("remitted"),您将句子分成两部分,可使用索引 0 和 1 寻址,但如果您想分隔第二部分的单词,您可以在空间上进行另一次拆分。例如:

    line.split('reminded',1)[1].split(" ") 
    

    通过这种方式,您将获得一个字符串列表,其中包含句子第二部分的单个单词。在此之后,您可以考虑从特殊字符、标点符号、ecc 中清除这些单词。按照此操作: Remove all special characters, punctuation and spaces from string

    【讨论】:

    • 如果你只想要句子第二部分的特定单词,分割后你可以按索引搜索它们,如果你事先知道的话,或者使用 for 检索索引
    【解决方案3】:
    text = "1. This is something abc Yes I like to swim No My Hair is real.\n2. i like to bike uphill and smile"
    
    split_text = text.split("\n", 1) #split on new line
    SomeWordFirstHalf = split_text[0].split()
    SomeWordSecondHalf = split_text[1].split()
    print(SomeWordFirstHalf[3])
    print(SomeWordSecondHalf[5])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-31
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 2022-11-26
      相关资源
      最近更新 更多