split 的输出不是我所期望的

发布于 2025-01-17 04:16:18 字数 1535 浏览 2 评论 0原文

我刚刚学习 python,在读取我创建的 .txt 文件时遇到一些问题。 我的目标: 我有一个包含字符串列表的 txt 文件。我正在尝试阅读、处理它并将每个字母保存到一个新列表中。


example2.txt 文件: [一、二、三、一、二、十、八、猫、狗、鸟、鱼] [阿隆索、艾丽西亚、鲍勃、林恩],[红、蓝、绿、粉、青]


我的输出 ['一、二、三、一、二、十、八、猫、狗、鸟、鱼]\n'] ['阿隆索,艾丽西亚,鲍勃,林恩],[红,蓝,绿,粉红,青色']

我期待的是这样的: ['一','二','三','一','二','十','八','猫','狗','鸟','鱼','阿隆索','Alicia','Bob','Lynn','red','blue','green','pink','cyan']

我的 python 代码 这就是我尝试过的;您可以忽略评论

import re
# Creating a variable to store later the contents of the file
list_String = []
# Reading the file
file = open("D:\dir\example2", "r")

for line in file:
    print(re.split('^[\s].', line.strip(' ][')))
    #list_String.append(line.strip('[]').strip("\n").split(","))
    #list_String = re.split(r'[^\S\t.]', line)
    #print(line.split(r"\S"))
    #print(line)

#print(list_String)

file.close()

我也在阅读有关如何使用 的文档re,但不知道是我的问题还是很难理解。

我尝试尝试我读到的内容,但我仍然没有得到我想要的。

我什至尝试了这个:

print(line.strip('][').strip('\n').strip(']').split(","))

输出

['one', ' two', ' THREE', ' one', ' two', ' ten', ' eight', 'cat', ' dog', ' bird', ' fish']
['Alonso', ' Alicia', ' Bob', ' Lynn] ', ' [red', ' blue', ' green', ' pink', ' cyan']

正如你所看到的,它确实有效。然而,在 Lynn 和 red 之间,大括号和逗号并没有以某种方式消失。

感谢您的时间和帮助

I'm just learning python, and I'm having some problems reading a .txt file that I created.
My objective:
I have a txt file with a list of strings. I'm trying to read, process it and save every letter into a new list.


example2.txt file:
[one, two, THREE, one, two, ten, eight,cat, dog, bird, fish] [Alonso, Alicia, Bob, Lynn] , [red, blue, green, pink, cyan]


My output
['one, two, THREE, one, two, ten, eight, cat, dog, bird, fish]\n']
['Alonso, Alicia, Bob, Lynn], [red, blue, green, pink, cyan']

What I was expecting was something like this:
['one','two','THREE','one','two','ten','eight','cat','dog','bird','fish','Alonso','Alicia','Bob','Lynn','red','blue','green','pink','cyan']

My code in python
This is what I tried; you can ignore the comments

import re
# Creating a variable to store later the contents of the file
list_String = []
# Reading the file
file = open("D:\dir\example2", "r")

for line in file:
    print(re.split('^[\s].', line.strip(' ][')))
    #list_String.append(line.strip('[]').strip("\n").split(","))
    #list_String = re.split(r'[^\S\t.]', line)
    #print(line.split(r"\S"))
    #print(line)

#print(list_String)

file.close()

I also was reading the documentation on how to use re, but I don't know if it is just me or is hard to understand.

I tried experimenting with what I read, but I'm still not getting what I wanted.

I even try this:

print(line.strip('][').strip('\n').strip(']').split(","))

Output

['one', ' two', ' THREE', ' one', ' two', ' ten', ' eight', 'cat', ' dog', ' bird', ' fish']
['Alonso', ' Alicia', ' Bob', ' Lynn] ', ' [red', ' blue', ' green', ' pink', ' cyan']

As you can see, it kind of works. However, between Lynn and red, the braces and the comma do not disappear somehow.

Thank you for the time and help

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

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

发布评论

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

评论(1

纵情客 2025-01-24 04:16:18

您可能会发现在模式 \w+ 上执行 re.findall 可以在这里工作:

inp = "[one, two, THREE, one, two, ten, eight,cat, dog, bird, fish] [Alonso, Alicia, Bob, Lynn] , [red, blue, green, pink, cyan]"
words = re.findall(r'\w+', inp)
print(words)

这将打印:

['one', 'two', 'THREE', 'one', 'two', 'ten', 'eight', 'cat', 'dog', 'bird', 'fish',
 'Alonso', 'Alicia', 'Bob', 'Lynn', 'red', 'blue', 'green', 'pink', 'cyan']

You might just find that doing an re.findall on the pattern \w+ works here:

inp = "[one, two, THREE, one, two, ten, eight,cat, dog, bird, fish] [Alonso, Alicia, Bob, Lynn] , [red, blue, green, pink, cyan]"
words = re.findall(r'\w+', inp)
print(words)

This prints:

['one', 'two', 'THREE', 'one', 'two', 'ten', 'eight', 'cat', 'dog', 'bird', 'fish',
 'Alonso', 'Alicia', 'Bob', 'Lynn', 'red', 'blue', 'green', 'pink', 'cyan']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文