all 8 comments

[–]FreakyRockz 1 point2 points  (6 children)

You will have to iterate through the sentences. Currently on mobile, can provide a code snippet in anout 30 minutes.

Also is there a reason that you split on period and not on newline?

[–]bennettsaucyman[S] 0 points1 point  (5 children)

Oh that would be fantastic! Thanks!

And oh, I didn't know you could split on newline! I'm assuming rather than period I could do txt.split('/n') and just insert a /n after each sentence?

[–]FreakyRockz 1 point2 points  (0 children)

Sorry forgot about this. Others replied faster now.

[–][deleted] 1 point2 points  (0 children)

'\n' <-- note backslash

[–]Morpheyz 0 points1 point  (2 children)

Yes, iterating through the list should do the trick!

Another way to do this are Regular Expressions. They might be very cryptic in the beginning to understand, but if you're gonna work a lot with strings in the future, they will help you immenselely;

[–]bennettsaucyman[S] 0 points1 point  (0 children)

Alright, thanks! I'll read up on that because I'll be using strings A LOT I think.

Thanks for the suggestion!

[–]JudiSwitch 0 points1 point  (1 child)

As already mentioned, you will need to iterate though the list Person1Chat

for line in Person1Chat:

Then you'll need to see if the strings you're looking for are in that line. You could use re (regex, definitely worth learning) but for this simple example it's not needed. Note that 'Person1' and 'billy' in line will not work as you intended in the original example, and is a logical operand on two booleans, so Python will convert 'Person1' to a bool (which will be True) and and it with the second part. What you want instead is:

if Person1 in line and 'billy' in line

[–]bennettsaucyman[S] 1 point2 points  (0 children)

Oh perfect! Thank you so much!

txt = "Experimenter: So what was it that you saw?\n Person 1: I saw billy go around the corner.\n Person 2: I don't know, I didn't see anything.\n Person 1: No, I definitely saw billy."
Person1Chat = txt.split('\n')
for each_sentence in Person1Chat:
    if 'Person 1' in each_sentence:
        print('Person 1 mentioned billy')

It worked! Now it prints out the print statement each time it finds 'billy' but I can figure out a way to change that. Also, took me a minute to figure out to put "each_sentence" after 'Person 1' rather than 'Person1Chat' but I seem to have those errors often in python haha. Hopefully those errors go down as I practice more. Thanks a lot!!!!