字符串拆分直到符号出现

时间:2019-12-16 10:08:09

标签: python string list split

我在列表中有以下项目:

class ViewController: UIViewController {

    @IBOutlet weak var mapAreaView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: self.mapAreaView.frame, camera: camera)
        self.mapAreaView.addSubview(mapView)
        do {
            // Set the map style by passing a valid JSON string.
            mapView.mapStyle = try GMSMapStyle(jsonString: kMapStyle)
        } catch {
            NSLog("One or more of the map styles failed to load. \(error)")
        }
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
        let imageIcon = UIImage(named: "pin (1)")
        marker.icon = imageIcon

    }
}

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

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

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

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

1 个答案:

答案 0 :(得分: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']