字符串拆分,直到符号出现

问题描述 投票:0回答:1

我在列表中有这些项目:

App,Category,Rating,Reviews,Size,Installs,Type,Price,Content Rating,Genres,Last Updated,Current Ver,Android Ver

Photo Editor & Candy Camera & Grid & ScrapBook,ART_AND_DESIGN,4.1,159,19M,"10,000+",Free,0,Everyone,Art & Design,"January 7, 2018",1.0.0,4.0.3 and up

Coloring book moana,ART_AND_DESIGN,3.9,967,14M,"500,000+",Free,0,Everyone,Art & Design;Pretend Play,"January 15, 2018",2.0.0,4.0.3 and up

"U Launcher Lite – FREE Live Cool Themes, Hide Apps",ART_AND_DESIGN,4.7,87510,8.7M,"5,000,000+",Free,0,Everyone,Art & Design,"August 1, 2018",1.2.4,4.0.3 and up

您可以看到数据被逗号(,)分割,但是对于逗号不能用作分隔符的特殊项目,该项目被放入了“”。

我想创建一个拆分列表,该列表将用逗号拆分所有项目,但放在“”中的项目除外。

到目前为止,这是我要分配第一个“和最后一个”的索引的代码。

for each in range(len(google_data)):

    start_index = google_data[each].find('\"')
    endIndex = google_data[each].find('\"', start_index + 1)
    word_b_quotes = google_data[each][start_index:endIndex]
python string list split
1个回答
1
投票

您可以按照以下方式使用csv模块,因为其格式完全相同:

from csv import reader

# test
input = ['A,B,C,"D12121",E,F,G,H,"I9,I8",J,K']

for item in reader(input):
    print item

# for the test input, prints
# ['A', 'B', 'C', 'D12121', 'E', 'F', 'G', 'H', 'I9,I8', 'J', 'K']
© www.soinside.com 2019 - 2024. All rights reserved.