2017-06-08 54 views
-2

如何通過「,」除python中的某些字符之間的字符串? 我的數據是這樣的:如何用「,」分隔字符串,除了在Python中的某些字符之間?

('00012+1357','LSC 2','Aa,Ab',2014,2014, 2,185,185, 0.2, 0.2,10.7,13.1,'M0.5',+019,+135,NULL,NULL,NULL,NULL,'000113.19+135830.3') 

我需要通過拆分他們 「」 除了'AA,AB'

結果應該是:

("00012+1357" "LSC 2" "Aa,Ab" "2014" "2014" "2" "185" "185" "0.2" "0.2" "10.7" "13.1" "M0.5" "+019" "+135" "NULL" "NULL" "NULL" "NULL" "000113.19+135830.3") 

你知道該怎麼做?

+5

我這樣做,但是有什麼*你*嘗試過。 – Pythonista

+0

你的數據看起來像一個元組......你究竟想要做什麼? 'join'? – depperm

+2

這個數據是一個'string'還是一個'list'? – KelvinS

回答

0

看來,你正在尋找「」。加入():

the_string = ('00012+1357','LSC 2','Aa,Ab',2014,2014, 2,185,185, 0.2, 0.2,10.7,13.1,'M0.5', 19, 135, 'NULL','NULL','NULL','NULL','000113.19+135830.3') 

the_string = map(str, the_string) 

new_string = (' '.join(i for i in the_string)) 
0

看來你試圖解析CSV數據。 csv模塊應該綽綽有餘,並且能夠處理所有這些邊界情況。

0

我會試穿一些代碼。 如果在quotes之外,則將其拆分爲needle。 假設needlequotes都是一個字符長。

#!python3 

def splitExceptBetween(istr, needle, quotes): 
    inside = -1 
    res = [] 
    oldt = 0 
    for index, letter in enumerate(istr): 
     if letter==quotes: 
      inside = -inside 
     elif letter==needle and inside == -1: 
      res.append(istr[oldt:index]) 
      oldt = index+1 
    if oldt<len(istr): 
     res.append(istr[oldt:]) 
    return res 

istr = "as'das'd.asdas'd.a'sdas.drth..rt'h.r'th.'" 
print(splitExceptBetween(istr, ".", "'")) 
istr = "00012+1357,LSC 2,'Aa,Ab',2014,2014, 2,185,185, 0.2, 0.2,10.7,13.1,M0.5,+019,+135,NULL,NULL,NULL,NULL,000113.19+135830.3" 
print(splitExceptBetween(istr, ",", "'")) 
相關問題