Search code examples
pythonlistfilescanning

Removing quotation marks from a .txt file


I'm trying to scan in a set of data from a text file. It's all on one line and all contained in quotation marks

["0887625941",";A3:McEI_nmFa","9727427353" etc.

I want to store them in an array on separate rows and remove the quotation marks

[0887625941
 ;A3:McEI_nmFa
 9727427353
 etc.

I have a set of code that on paper is supposed to do exactly that but in reality seems to remove nearly all the code to the point where it's indistinguishable

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
"
]

Any ideas as to what I'm doing wrong?


Solution

  • See below (assuming a.txt contains your data)

    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)
    

    output

    [0887625941
    ;A3:McEI_nmFa
    9727427353
    ['[0887625941', ';A3:McEI_nmFa', '9727427353']