2017-09-15 44 views
3

刪除單引號我有一個輸入字符串:從列表在Python

result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"' 

data = result.split("\n") 

i = 0 
while i < len(data): 
    i = i +1 
    dd = data[i].split(',') 
    print dd 
    break 

和相應的輸出爲:

[ 
    '"testing"', 
    '"0.8841"', 
    '"642000.0"', 
    '"80.014521"', 
    '"-60.940653"', 
    '"4522126666"', 
    '"1500854400"', 
    '""', 
    '"1500842014000"', 
    '"name"', 
    '"80.014521"', 
    '"-60.996532"', 
    '"sampledevice"', 
    '"3"', 
    '"name"' 
] 

我怎樣才能在列表中的每個元素去掉單引號?

+0

注:'而我 AChampion

+0

謝謝@AChampion –

回答

5

很好地解決對待文本一個CSV:

import csv 
import StringIO 

result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"' 
print next(csv.reader(StringIO.StringIO(result))) 

給你:

['testing', '0.8841', '642000.0', '80.014521', '-60.940653', '4522126666', '1500854400', '', '1500842014000', 'name', '80.014521', '-60.996532', 'sampledevice', '3', 'name'] 
+0

這有什麼好處是,它允許在字符串中的逗號以及將它們分開。 – Stael

5

您需要應用strip刪除字符串兩邊的引號。

dd = [x.strip('"') for x in data[i].split(',')] 

說,你的循環似乎有索引問題。應該被重寫,比如像這樣:

result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400"\n"1500842014000","name","80.014521","-60.996532","sampledevice","3","name"' 

for line in result.splitlines(): 
    dd = [x.strip('"') for x in line.split(',')] 
    print(dd) 

在這一點上,你會甚至

dd = ast.literal_eval(line) 

csv模塊還完美的使用最好用list作爲輸入(無需通過一個文件句柄)(不通過一個string,不過,因爲它產生了一些奇怪的效果)

導入CSV

for row in csv.reader(result.splitlines()): 
    print(row) 

所有導致:

['testing', '0.8841', '642000.0', '80.014521', '-60.940653', '4522126666', '1500854400'] 
['1500842014000', 'name', '80.014521', '-60.996532', 'sampledevice', '3', 'name'] 
4

更換雙引號前分裂

>>> result.replace('"', '').split(',') 
['testing', '0.8841', '642000.0', '80.014521', '-60.940653', '4522126666', '1500854400', '', '1500842014000', 'name', '80.014521', '-60.996532', 'sampledevice', '3', 'name'] 
+0

Downvoter喜歡發表評論嗎? –

+0

NMDV但如果字符串中有引號就有問題。當然,在OP嘗試中,如果字符串中有逗號,它也不起作用。 –

+0

@ Jean-FrançoisFabre誠然,但那些雙引號不是單引號。如果不存在,則日常語法中的雙引號內的雙引號很少見。 –

4

literal_eval針對此問題

import ast 
dd = [ast.literal_eval(i) for i in data] 
1

第一:你不必在輸出字符串單引號。你有什麼是用雙引號引用的字符串(可能用於存儲在csv文件中)。單引號您在輸出中看到的實際上是確定字符串。所以:

'"some_string"' 

實際上是

"some_string" 

二:如果你想刪除雙引號,你可以這樣做:

for item in data.split(","): 
    print(item.strip('"')) 

請看下面的語句,它應該是比較清楚:

item = '"some_string"' 
other_item = 'some_string' 
some_other_item = "some_string" 

print(item) # --> "some_string" 
print(other_item) # --> some_string 
print(some_other_item) # --> some_string 

print(item, other_item, some_other_item) # --> ('"some_string"', 'some_string', 'some_string') 
+0

是@Fejs結果與csv文件完全一致。感謝您的建議。它非常有幫助 –

1

雖然政治家t很奇怪。他不想刪除單引號嗎?爲什麼每個人都會發布刪除雙引號的答案。

這裏是我的建議:

result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"' 

data = result.split("\n") 

s = str(data)[2:-2] #Convert to a string, and delete [' and '] 
for dd in s.split(','): 
    print dd 

結果:

"testing" 
"0.8841" 
"642000.0" 
"80.014521" 
"-60.940653" 
"4522126666" 
"1500854400" 
"" 
"1500842014000" 
"name" 
"80.014521" 
"-60.996532" 
"sampledevice" 
"3" 
"name" 
+0

你讀過這個問題嗎? –

+0

@ Jean-FrançoisFabre實際上Alperen有一點 - 由於OP描述問題的方式... – coder

+0

@coder如果你這樣說。嘗試理解太累了。我剛剛編輯「無法評論」,因爲這是一個實際的答案。 –