Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python通过将字符串拆分为列表并分隔元素来计算字符串的长度_Python - Fatal编程技术网

Python通过将字符串拆分为列表并分隔元素来计算字符串的长度

Python通过将字符串拆分为列表并分隔元素来计算字符串的长度,python,Python,我的问题涉及在文本中查找包含分号的句子,并查找分号前后的单词数。我知道如何用分号分割所有内容,但是我得到了两个字符串,但我似乎无法计算字符串中的单词 文本如下所示: "What does Bessie say I have done?" I asked. "Jane, I don't like cavillers or questioners; besides, there is something truly forbidding in a child taking up her elders

我的问题涉及在文本中查找包含分号的句子,并查找分号前后的单词数。我知道如何用分号分割所有内容,但是我得到了两个字符串,但我似乎无法计算字符串中的单词

文本如下所示:

"What does Bessie say I have done?" I asked.
"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."  
A breakfast-room adjoined the drawing-room, I slipped in there.
It contained a bookcase: I soon possessed myself of a volume, taking care that it should be one stored with pictures.
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.
Folds of scarlet drapery shut in my view to the right hand; to the left were the clear panes of glass, protecting, but not separating me from the drear November day.
At intervals, while turning over the leaves of my book, I studied the aspect of that winter afternoon.
Afar, it offered a pale blank of mist and cloud; near a scene of wet lawn and storm-beat shrub, with ceaseless rain sweeping away wildly before a long and lamentable blast.
I returned to my book--Bewick's History of British Birds: the letterpress thereof I cared little for, generally speaking; and yet there were certain introductory pages that, child as I was, I could not pass quite as a blank.
They were those which treat of the haunts of sea-fowl; of "the solitary rocks and promontories" by them only inhabited of the coast of Norway, studded with isles from its southern extremity, the Lindeness, or Naze, to the North Cape--     "Where the Northern Ocean, in vast whirls,    
Boils round the naked, melancholy isles
Of farthest Thule; and the Atlantic surge
Pours in among the stormy Hebrides."
Nor could I pass unnoticed the suggestion of the bleak shores of Lapland, Siberia, Spitzbergen, Nova Zembla, Iceland, Greenland, with "the vast sweep of the Arctic Zone, and those forlorn regions of dreary space,--that reservoir of frost and snow, where firm fields of ice, the accumulation of centuries of winters, glazed in Alpine heights above heights, surround the pole, and concentre the multiplied rigours of extreme cold."  
Of these death-white realms I formed an idea of my own: shadowy, like all the half-comprehended notions that float dim through children's brains, but strangely impressive.
到目前为止,我取得了以下成就:

count = -1
for line in open("jane_eyre_sentences.txt"):
  words = line.strip("\n")
  count += 1
  if ";" in words:
    wordssplit = words.split(";")


    print("Line " +str(count)+ ":", wordssplit )
我使用计数的原因是每次迭代后计数增加1,因此句子被标记。我已经去掉了句子末尾的段落,如果句子中包含分号,我也用分号将它们分开

到目前为止,我只试着打印单词split,看看它能给我带来什么

Line 1: ['"Jane, I don\'t like cavillers or questioners', ' besides, there is something truly forbidding in a child taking up her elders in that manner.']
Line 2: ['Be seated somewhere', ' and until you can speak pleasantly, remain silent."  ']
Line 5: ['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.']
Line 6: ['Folds of scarlet drapery shut in my view to the right hand', ' to the left were the clear panes of glass, protecting, but not separating me from the drear November day.']
Line 8: ['Afar, it offered a pale blank of mist and cloud', ' near a scene of wet lawn and storm-beat shrub, with ceaseless rain sweeping away wildly before a long and lamentable blast.']
Line 9: ["I returned to my book--Bewick's History of British Birds: the letterpress thereof I cared little for, generally speaking", ' and yet there were certain introductory pages that, child as I was, I could not pass quite as a blank.']
Line 10: ['They were those which treat of the haunts of sea-fowl', ' of "the solitary rocks and promontories" by them only inhabited of the coast of Norway, studded with isles from its southern extremity, the Lindeness, or Naze, to the North Cape--     "Where the Northern Ocean, in vast whirls,    ']
Line 12: ['Of farthest Thule', ' and the Atlantic surge']

我不知道你的目的是什么,但据我所知,你只是想知道一个文件中每一行的单词数在半表的左边和右边?对的如果是这样的话,这应该对你有用

with open(textfile,'rt',encoding='utf-8')as infile:
    for line in infile:
        for i,e in enumerate(line.split(' ')):
            if e.endswith(';'):
                print("--> {}\nContains {} words to the left, and {} words to the right\n".format(line.strip(),i+1,len(line.strip().split(" "))-(i+1)))

那么您想要的输出是什么呢?我认为您必须使用正则表达式来捕获结尾带有分号的单词,即紧跟分号但带有空格的单词。那会更容易。可能是重复的