我正在尝试从文本文件中扫描一组数据。全部都在一行上,并且都包含在引号中
["0887625941",";A3:McEI_nmFa","9727427353" etc.
我想将它们存储在单独行中的数组中,并删除引号
[0887625941
;A3:McEI_nmFa
9727427353
etc.
我在纸上应该有一组代码可以做到这一点,但实际上似乎将几乎所有代码都删除到难以区分的程度
dataList = []
result = open('data.txt')
for i in result:
result = i.strip().split(',')
for i in result:
result = i.replace('"', '')
for i in result:
dataList.append(i)
for i in dataList:
print(i)
Output:
"
Q
0
t
Q
y
r
h
g
e
r
^
J
m
^
9
v
M
]
n
"
]
关于我在做什么错的任何想法?
with open('a.txt') as f:
data = f.read().replace('"','').replace(',','\n')
print(data)
with open('a.txt') as f:
# OR - if you want to store the elements in a list, just do
lst = f.read().replace('"','').split(',')
print(lst)
输出
[0887625941
;A3:McEI_nmFa
9727427353
['[0887625941', ';A3:McEI_nmFa', '9727427353']