Python tách chuỗi csv thành danh sách
Fergus, cậu bé 10 tuổi, đang học lập trình với Python. Như bài tập về nhà, giáo viên đã yêu cầu cả lớp tạo một chuỗi được phân tách bằng dấu phẩy và chuyển đổi chuỗi này thành một pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)3. Fergus cần sự giúp đỡ của bạn Show 💬 câu hỏi. Chúng ta sẽ viết mã Python như thế nào để chuyển đổi một chuỗi được phân tách bằng dấu phẩy thành một pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)3? Chúng ta có thể hoàn thành nhiệm vụ này bằng một trong các tùy chọn sau
Phương pháp 1. Sử dụng chia()Phương pháp này sử dụng pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)5 để chia Chuỗi thành các phần tử dựa trên dấu phân cách. Sau đó, pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)5 trả về một pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)8 chứa các phần tử tương tự pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list) Ở trên, tạo một chuỗi được phân tách bằng dấu phẩy chứa sáu (6) tên thú cưng và lưu chúng vào pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)9 Tiếp theo, pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)5 được thêm vào pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)9 và thông qua dấu phân cách ( import re pet_ages = "12, 4, 8, 2, 1, 7" pet_list = [int(i) for i in re.findall(r'\d+', pet_ages)] print(pet_list)2)). Điều này cho biết để pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)5 ngắt chuỗi pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)9 thành các đoạn (phần tử) nhỏ hơn tại ký tự đã nói ( import re pet_ages = "12, 4, 8, 2, 1, 7" pet_list = [int(i) for i in re.findall(r'\d+', pet_ages)] print(pet_list)5) Điều này dẫn đến một pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)8 chứa sáu (6) phần tử chuỗi được xuất ra thiết bị đầu cuối[‘Chewie’, ‘Peppy’, ‘Elvis’, ‘Axel’, ‘Banjo’, ‘Draper’] Phương thức chuỗi Python [Hướng dẫn cơ bản] Xem video này trên YouTube Phương pháp 2. Sử dụng Danh sách hiểu và tách()Phương pháp này sử dụng pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)6 kết hợp với pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)5 để chuyển đổi một chuỗi được phân tách bằng dấu phẩy thành một danh sách pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list) Ở trên, tạo một chuỗi được phân tách bằng dấu phẩy chứa sáu (6) tên thú cưng và lưu chúng vào pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)9 Tiếp theo, pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)6 được sử dụng để lặp qua pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)9 và chia chuỗi thành các phần (phần tử) nhỏ hơn tại ký tự đã nói ( import re pet_ages = "12, 4, 8, 2, 1, 7" pet_list = [int(i) for i in re.findall(r'\d+', pet_ages)] print(pet_list)5). Điều này dẫn đến một pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)8 chứa sáu (6) phần tử chuỗi Nội dung của pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)54 được xuất ra thiết bị đầu cuối[‘Chewie’, ‘Peppy’, ‘Elvis’, ‘Axel’, ‘Banjo’, ‘Draper’] Thủ thuật Python One-Liner 9 - Hiểu danh sách lồng nhau Xem video này trên YouTube Phương pháp 3. Sử dụng Danh sách hiểu và findall()Phương pháp này nhập thư viện pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)55 và gọi hàm pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)2 để định vị tất cả các lần xuất hiện của một ký tự được chỉ định và phân tách tương ứng import re pet_ages = "12, 4, 8, 2, 1, 7" pet_list = [int(i) for i in re.findall(r'\d+', pet_ages)] print(pet_list) Ở trên, nhập thư viện pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)55. Bấm vào đây nếu điều này yêu cầu cài đặt Tiếp theo, chúng tôi tạo một chuỗi được phân tách bằng dấu phẩy chứa sáu (6) độ tuổi của thú cưng và lưu các độ tuổi này vào pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)58ages Sử dụng pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)6, chúng tôi lặp qua pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)10, tìm tất cả các lần xuất hiện của một hoặc nhiều chữ số ( pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)11‘), chia chuỗi một cách thích hợp, chuyển đổi thành số nguyên và lưu vào pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)54. Điều này dẫn đến một pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)8 chứa sáu (6) phần tử số nguyên Nội dung của pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)54 được xuất ra thiết bị đầu cuối[12, 4, 8, 2, 1, 7] 5 kỹ năng phải có để trở thành lập trình viên (mà bạn chưa biết) Xem video này trên YouTube Phương pháp 4. Sử dụng Danh sách hiểu và bản đồ ()Phương pháp này sử dụng pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)6 kết hợp với pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)5 và pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)5 để chuyển đổi một chuỗi được phân tách bằng dấu phẩy thành một danh sách pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)5 Ở trên, chúng tôi tạo một chuỗi được phân tách bằng dấu phẩy chứa sáu (6) độ tuổi của thú cưng và lưu chúng vào pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)10 Hàm pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)5 được sử dụng và chấp nhận hai (2) đối số. một loại dữ liệu ( pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)10) và một loại có thể lặp lại ( pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)11). Điều này sau đó được chuyển đổi thành một đối tượng pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)5 tương tự như bên dưới<đối tượng bản đồ tại 0x0000018D8AC6AA70> Tiếp theo, sử dụng pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = pet_names.split(',') print(pet_list)6, chúng tôi chuyển đổi đối tượng này thành danh sách các số nguyên và xuất nó ra thiết bị đầu cuối[12, 4, 8, 2, 1, 7] Làm chủ chức năng bản đồ Python [+Video] Xem video này trên YouTube Thưởng. Chuyển đổi thành TupleTrong một số trường hợp, bạn có thể muốn chuyển đổi một chuỗi được phân tách bằng dấu phẩy thành một bộ pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)1 Ở trên, tạo một chuỗi được phân tách bằng dấu phẩy chứa sáu (6) tên thú cưng và lưu chúng vào pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)9 Tiếp theo, tuple được gọi và thông qua một đối số ( pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper' pet_list = [x for x in pet_names.split(',')] print(pet_list)15). Câu lệnh này chia chuỗi thành các phần (phần tử) nhỏ hơn dựa trên dấu phân cách Điều này dẫn đến một bộ chứa sáu (6) phần tử chuỗi được xuất ra thiết bị đầu cuối (‘Chewie’, ‘Peppy’, ‘Elvis’, ‘Axel’, ‘Banjo’, ‘Draper’)Hướng dẫn cơ bản về Python Tuples. Giới thiệu và cơ sở lý thuyết. Phần 1/7 Xem video này trên YouTube Bản tóm tắtCác phương pháp chuyển đổi chuỗi được phân tách bằng dấu phẩy thành danh sách này sẽ cung cấp cho bạn đủ thông tin để chọn chuỗi tốt nhất cho yêu cầu viết mã của bạn Chúc may mắn và mã hóa hạnh phúc lập trình viên👱♀️ Lập trình viên 1. Chúng tôi gặp sự cố … chưa hết – bạn có thể dễ dàng giảm hai vấn đề này xuống 0 khi hoàn thiện “Siêu năng lực RegEx trong Python” của mình. 🙂 |