split 的输出不是我所期望的
我刚刚学习 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会发现在模式
\w+
上执行re.findall
可以在这里工作:这将打印:
You might just find that doing an
re.findall
on the pattern\w+
works here:This prints: