0

I am new to python and I would like to print different words from the sentence. Below is the text file

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.

From the above text file, I want 10th word(few), 11th word(people), 39th word(morning), 43rd word (1969), from the first line. And for the second line also 10th word(majority), 11th word (people), 39th word (evening), 43rd (1979).

Note: In the Test.txt 1. means line one and 2. means line 2

For this, I tried using the split function.

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

This is the output that I got

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.

How do I print only one word after using split function? I am pretty that I still need to improve this code.

Any help would be appreciated. Thanks in advance.

sri
  • 75
  • 8

3 Answers3

1

here is your code:


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)
Dev
  • 387
  • 1
  • 12
  • output: File "E:/python files/try.py", line 13, in outputlist = file.replace('.','').split('reminded', 1)[1].split(" ") AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'. I got an error while trying to execute this code. Is there any specific reason or this such as python version..... – sri Jun 04 '20 at 06:58
  • I tried the above code but it threw me the error. Can you please post the output which you got If possible. – sri Jun 04 '20 at 07:33
  • few morning 1969 majority evening 1979 - this is my output – Dev Jun 04 '20 at 07:39
  • give your appreciation using rating – Dev Jun 04 '20 at 07:43
0

Using split("reminded"), you splitted your sentence into two pieces addressable using indexes 0 and 1, but if you want to separate the words of the second part you can do another split on space. For example:

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

In this way you will have a list of string which contains the single words of the second part of your sentence. After this you can think about clean these words from special characters, punctuation, ecc. Follow this in order to do it: Remove all special characters, punctuation and spaces from string

Ennio
  • 153
  • 2
  • 11
  • If you want only specific words of the second part of your sentence, after the split you can search them by index, if you know it in advance or retrieve a index using a for – Ennio Jun 04 '20 at 06:24
0
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])
Brett La Pierre
  • 493
  • 6
  • 15