使用 python 将文本文件中的所有单词组织到数组或链表中

发布于 2024-11-18 10:25:39 字数 885 浏览 2 评论 0原文

我有一个以下格式的文本文件。

<InitialNode>-><MergeNode *merge>->("Prepare for election")
->["Equipment, voter lists, ballot styles and/or ballots"]-><ForkNode>
{ ->("Prepare for voting (precinct)")-><ForkNode>
{ ->("Gather in-person vote") // Includes early voting.
->["Ballots and/or ballot images"]->(Collect *c),
"Precinct count"->("Count (precinct count)")
->["Machine totals"]->0..1(*c)
}

我需要分割文本文件并以这样的方式存储元素,

array[0]=<InitialNode>
array[1]=->
array[2]=<MergeNode *merge>
array[3]=->
array[4]=("Prepare for election")
array[5]=-> 
array[6]=["Equipment, voter lists, ballot styles and/or ballots"]
array[7]=->

依此类推。

总之我需要在->的基础上分割整个文本。

'->' 之间的所有内容, ',' , '}' , '}' 必须存储在链表中或 大批。

我怎样才能使用 python 来做到这一点?

I have a text file of the following format.

<InitialNode>-><MergeNode *merge>->("Prepare for election")
->["Equipment, voter lists, ballot styles and/or ballots"]-><ForkNode>
{ ->("Prepare for voting (precinct)")-><ForkNode>
{ ->("Gather in-person vote") // Includes early voting.
->["Ballots and/or ballot images"]->(Collect *c),
"Precinct count"->("Count (precinct count)")
->["Machine totals"]->0..1(*c)
}

I need to split the text file and store the elements in such a way that

array[0]=<InitialNode>
array[1]=->
array[2]=<MergeNode *merge>
array[3]=->
array[4]=("Prepare for election")
array[5]=-> 
array[6]=["Equipment, voter lists, ballot styles and/or ballots"]
array[7]=->

and so on.

In short I need to split the entire text on the basis of ->.

All the things that comes between '->' , ',' , '}' , '}' must be stored in a linked list or
array.

How could I do this using python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-11-25 10:25:39

如果你的初始字符串存储在 x.

要获取 x 中的所有文本,请这样做

x = open("/tmp/filename").read()

这应该可以解决问题

import re
splitter = re.compile('->')
array = splitter.split(x)

If your initial string is stored in x.

To get all the text in x,do

x = open("/tmp/filename").read()

This should do the trick

import re
splitter = re.compile('->')
array = splitter.split(x)
寒江雪… 2024-11-25 10:25:39

我只会使用字符串的内置 split 方法。

>>> with open('txtfile.txt', 'r') as f:
...     txt = f.read()
... 
>>> txt.split('->')
['\n<InitialNode>', '<MergeNode *merge>', '("Prepare for election")\n', 
 '["Equipment, voter lists, ballot styles and/or ballots"]', '<ForkNode>\n{ ', 
 '("Prepare for voting (precinct)")', '<ForkNode>\n{ ', 
 '("Gather in-person vote") // Includes early voting.\n', 
 '["Ballots and/or ballot images"]', '(Collect *c),\n"Precinct count"', 
 '("Count (precinct count)")\n', '["Machine totals"]', '0..1(*c)\n}\n'
]

但我不确定你所说的“'->'之间的所有事物”是什么意思, ',' , '}' , '}' 必须存储在链表或数组中。"这个有这个作用吗?或者您需要进一步拆分这些子字符串吗?

I would just use the built-in split method of strings.

>>> with open('txtfile.txt', 'r') as f:
...     txt = f.read()
... 
>>> txt.split('->')
['\n<InitialNode>', '<MergeNode *merge>', '("Prepare for election")\n', 
 '["Equipment, voter lists, ballot styles and/or ballots"]', '<ForkNode>\n{ ', 
 '("Prepare for voting (precinct)")', '<ForkNode>\n{ ', 
 '("Gather in-person vote") // Includes early voting.\n', 
 '["Ballots and/or ballot images"]', '(Collect *c),\n"Precinct count"', 
 '("Count (precinct count)")\n', '["Machine totals"]', '0..1(*c)\n}\n'
]

But I'm not sure what you mean by "All the things that comes between '->' , ',' , '}' , '}' must be stored in a linked list or array." Does this do that? Or do you need to split up these substrings further?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文