D.Ronald
D.Ronald

Reputation: 172

Splitting elements within a list and separate strings, then counting the length

If I have several lines of code, such that

"Jane, I don't like cavillers or questioners; besides, there is something truly forbidding in a child taking up her elders in that manner.
Be seated somewhere; and until you can speak pleasantly, remain silent."  
I mounted into the window- seat: gathering up my feet, I sat cross-legged, like a Turk; and, having drawn the red moreen curtain nearly close, I was shrined in double retirement.

and I want to split the 'string' or sentences for each line by the ";" punctuation, I would do

for line in open("jane_eyre_sentences.txt"):
  words = line.strip("\n")
  words_split = words.split(";")

However, now I would get strings of text such that,

["Jane, I don't like cavillers or questioners', 'besides, there is something truly forbidding in a child taking up her elders in that manner.']
[Be seated somewhere', 'and until you can speak pleasantly, remain silent."']  
['I mounted into the window- seat: gathering up my feet, I sat cross-legged, like a Turk', 'and, having drawn the red moreen curtain nearly close, I was shrined in double retirement.']

So it has now created two separate elements in this list.

How would I actually separate this list.

I know I need a 'for' loop because it needs to process through all the lines. I will need to use another 'split' method, however I have tried "\n" as well as ',' but it will not generate an answer, and the python thing says "AttributeError: 'list' object has no attribute 'split'". What would this mean?

Once I separate into separate strings, I want to calculate the length of each string, so i would do len(), etc.

Upvotes: 1

Views: 99

Answers (1)

naechtner
naechtner

Reputation: 110

You can iterate through the list of created words like this:

for line in open("jane_eyre_sentences.txt"):
  words = line.strip("\n")
  for sentence_part in words.split(";"):
    print(sentence_part) # will print the elements of the list
    print(len(sentence_part) # will print the length of the sentence parts

Alernatively if you just need the length for each of the parts:

for line in open("jane_eyre_sentences.txt"):
  words = line.strip("\n")
  sentence_part_lengths = [len(sentence_part) for sentence_part in words.split(";")]

Edit: With further information from your second post.

for count, line in enumerate(open("jane_eyre_sentences.txt")):
  words = line.strip("\n")
  if ";" in words:
    wordssplit = words.split(";")
    number_of_words_per_split = [(x, len(x.split())) for x in wordsplit]
    print("Line {}: ".format(count), number_of_words_per_split)

Upvotes: 1

Related Questions