在Python中使用两个定界符从字符串数据创建嵌套列表 [英] Creating nested list from string data with two delimiters in Python
问题描述
我正在尝试获取一个如下所示的文本文件:
I am trying to take a text file that looks like this:
1〜氢〜H〜1.008〜1〜1 | 2〜氦〜氦〜4.002〜18〜1 | 3〜锂〜锂〜6.94〜1〜2 | 4〜铍 是〜9.0122〜2〜2 |
1~Hydrogen~H~1.008~1~1|2~Helium~He~4.002~18~1|3~Lithium~Li~6.94~1~2|4~Beryllium~ Be~9.0122~2~2|
并将其转换为如下所示的嵌套列表:
and turn it into a nested list that looks like this:
[[1,氢,H,1.008,1,1],[2,氦,He,4.002,18,1],[3,锂,Li,6.94,1,2],[4,铍,Be,9.0122,2,2]]
[[1, Hydrogen, H, 1.008, 1, 1], [2, Helium, He, 4.002, 18, 1], [3, Lithium, Li, 6.94, 1, 2], [4, Beryllium, Be, 9.0122, 2, 2]]
我的代码如下:
class Parser:
def __init__(self, path):
self.file = open(path, "r")
self.unparsed_info = self.file.read()
self.parsed_by_element = []
self.parsed_info = []
self.parse_list('|', '~')
def parse_list(self, delimiter1, delimiter2):
for elements in self.unparsed_info.split(delimiter1):
e = elements.strip(delimiter1)
if e != '':
self.parsed_by_element.append(e)
for properties in e.split(delimiter2):
p = properties.strip(delimiter2)
if p != '':
self.parsed_by_element.insert("something that represents location of current element being manipulated", p)
但是我不知道如何在最后一行插入空白.有人有什么建议吗?还是更好的方法呢?
but I can't figure out how to fill in the blank for the insertion on the last line. Does anybody have any suggestions? Or a better way to do this?
推荐答案
您可以用一种更简单的方法来完成此操作,同时我假设您需要进行int和float转换吗?因为您想要的输出看起来就是这样.
You can do this in a much simpler way, also I'm assuming you need int and float conversions? because your desired output looks that way.
def parse(path):
list_of_lists = []
with open(path) as file_handle:
for line in file_handle:
for string in line.split("|"):
if string:
list_of_lists.append([int(e) if e.isdigit() else float(e) if "." in e else str(e) for e in string.rstrip().split("~") if e != " "])
return list_of_lists
my_filepath = "mytxt.txt"
my_list_of_lists = parse(my_filepath)
结果:
for sublist in my_list_of_lists:
print (sublist)
[1, 'Hydrogen', 'H', 1.008, 1, 1]
[2, 'Helium', 'He', 4.002, 18, 1]
[3, 'Lithium', 'Li', 6.94, 1, 2]
[4, 'Beryllium', ' Be', 9.0122, 2, 2]
这篇关于在Python中使用两个定界符从字符串数据创建嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!