簡體   English   中英

從 a.txt 文件中刪除引號

[英]Removing quotation marks from a .txt file

我正在嘗試從文本文件中掃描一組數據。 它都在一行上,並且都包含在引號中

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

關於我做錯了什么的任何想法?

見下文(假設 a.txt 包含您的數據)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM