Để tạo danh sách
[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
0 từ dữ liệu CSV đó, bạn cần chuyển đổi chuỗi số thành số nguyên. Đây là một cách để làm điều đó trong Python 2.import csv
data = []
with open['sample.csv', 'rb'] as csvfile:
reader = csv.reader[csvfile, skipinitialspace=True]
data.append[tuple[next[reader]]]
for num, val in reader:
data.append[[int[num], val]]
print data
đầu ra
[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
1 mang lại cho mỗi hàng dữ liệu CSV dưới dạng danh sách. Chúng ta cần trích xuất các mục từ danh sách đó và chuyển đổi cái đầu tiên thành [['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
2. Và sau đó chúng ta có thể đóng gói hai mục thành một bộ phận và nối nó vào danh sách [['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
3. Tuy nhiên, dòng tiêu đề chứa hai chuỗi, vì vậy chúng tôi không muốn thực hiện chuyển đổi đó trên đường tiêu đề.Mã này:
data.append[tuple[next[reader]]]
Nhận dòng tiêu đề và chỉ cần chuyển đổi nó thành một bộ dữ liệu và nối kết quả vào danh sách
[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
3 của chúng tôi.Lưu ý rằng mô -đun Python 2
[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
5 yêu cầu bạn mở tệp ở chế độ [['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
6, nhưng trong Python 3, bạn cần mở tệp ở chế độ [['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
7. Vui lòng xem các tài liệu mô -đun tương ứng để biết thêm chi tiết. Ngoài ra, và sử dụng hàm [['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
8 thay vì câu lệnh [['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
8, không cần thay đổi nào khác để chạy mã trên trên Python 3. Làm cách nào để biến một danh sách thành một chuỗi trong Python? Để chuyển đổi danh sách thành một chuỗi, hãy sử dụng khả năng hiểu danh sách Python và hàm tham gia []. Sự hiểu biết danh sách sẽ đi qua từng phần tử một và phương thức tham gia [] sẽ kết hợp các phần tử của danh sách thành một chuỗi mới và trả về nó làm đầu ra. Tiếp theo, bạn sẽ thấy các bước để áp dụng mẫu trên trong thực tế. Ví dụ: hãy để chuẩn bị một tệp CSV [được gọi là ‘sản phẩm‘] có chứa các thông tin sau:Products‘] that
contains the following information: Nếu bạn đã thực hiện như vậy, hãy cài đặt gói Pandas. Bạn có thể sử dụng cú pháp sau để cài đặt gói Pandas trong Windows: Bây giờ bạn có thể sử dụng mẫu sau để hỗ trợ bạn trong quá trình chuyển đổi tệp CSV sang chuỗi JSON: Ví dụ:import pandas as pd
df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv']
df.to_json [r'Path where the new JSON file will be stored\New File Name.json']
Bạn có thể sử dụng mẫu sau để chuyển đổi CSV thành chuỗi JSON bằng Python:
Sản phẩm
Giá bán
Máy tính desktop 700 Máy tính bảng 250 Máy in 120 Máy tính xách tay 1200 Bước 2: Cài đặt gói Pandas
pip install pandas
Bước 3: Chuyển đổi CSV thành Chuỗi JSON bằng Python
import pandas as pd
df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv']
df.to_json [r'Path where the new JSON file will be stored\New File Name.json']
- Đường dẫn mà tệp CSV được lưu là: C: \ Users \ Ron \ Desktop \ test \ Products.csvC:\Users\Ron\Desktop\Test\Products.csv
- Trong đó ‘Sản phẩm là tên tệp và‘ CSV là tiện ích mở rộng tệpProducts‘ is the file name, and ‘csv‘ is the file extension
- Đường dẫn nơi tệp JSON mới sẽ được lưu trữ là: C: \ Users \ ron \ Desktop \ test \ new_products.jsonC:\Users\Ron\Desktop\Test\New_Products.json
- Trong đó ‘new_products là tên tệp mới và‘ json là tiện ích mở rộng tệpNew_Products‘ is the new file name, and ‘json‘ is the file extension
Bạn cần phải sửa đổi các đường dẫn đến vị trí nơi các tệp sẽ được lưu trữ trên máy tính của bạn.
Dưới đây là mã Python hoàn chỉnh để chuyển đổi tệp CSV thành chuỗi JSON cho ví dụ của chúng tôi:
import pandas as pd df = pd.read_csv [r'C:\Users\Ron\Desktop\Test\Products.csv'] df.to_json [r'C:\Users\Ron\Desktop\Test\New_Products.json']
Chạy mã trong Python [được điều chỉnh theo đường dẫn của bạn] và tệp JSON mới sẽ được tạo tại vị trí được chỉ định của bạn.
Nếu bạn mở tệp JSON, bạn sẽ thấy chuỗi sau:
{"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":120,"3":1200}}
Bạn cũng có thể muốn kiểm tra các hướng dẫn sau giải thích cách chuyển đổi:
- Chuỗi JSON thành CSV
- Excel đến CSV
- CSV đến Excel
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Approach:
- Bàn luận
- Trong bài viết này, chúng ta sẽ xem cách chuyển đổi các cột CSV thành văn bản bằng Python và chúng ta cũng sẽ xem cách chuyển đổi tất cả các cột CSV thành văn bản.
- Đọc tệp .csv bằng cách sử dụng gấu trúc DataFrame.
Chuyển đổi cột cụ thể thành danh sách bằng hàm tạo Danh sách []
Code:
Sau đó, tuần tự chuyển đổi từng phần tử của danh sách thành một chuỗi và tham gia chúng bằng một ký tự hoặc không gian cụ thể.
Đối với chương trình của chúng tôi, chúng tôi sẽ sử dụng tệp CSV sau:
Python3
data.append[tuple[next[reader]]]
0 data.append[tuple[next[reader]]]
1data.append[tuple[next[reader]]]
2data.append[tuple[next[reader]]]
3 data.append[tuple[next[reader]]]
4data.append[tuple[next[reader]]]
5data.append[tuple[next[reader]]]
6[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
8pip install pandas4
data.append[tuple[next[reader]]]
7data.append[tuple[next[reader]]]
3 data.append[tuple[next[reader]]]
9import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']0
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']1
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']2
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']3
data.append[tuple[next[reader]]]
3 import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']5
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']6
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']7
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']8
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']9
[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
8import pandas as pd df = pd.read_csv [r'C:\Users\Ron\Desktop\Test\Products.csv'] df.to_json [r'C:\Users\Ron\Desktop\Test\New_Products.json']0
Output:
Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 200
pip install pandas
5data.append[tuple[next[reader]]]
3 data.append[tuple[next[reader]]]
9import pandas as pd
df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv']
df.to_json [r'Path where the new JSON file will be stored\New File Name.json']
0pip install pandas
9import pandas as pd
df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv']
df.to_json [r'Path where the new JSON file will be stored\New File Name.json']
2
data.append[tuple[next[reader]]]
data.append[tuple[next[reader]]]
pip install pandas0
data.append[tuple[next[reader]]]
3 import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']3
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']6
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']5
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']6
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']7
import pandas as pd df = pd.read_csv [r'Path where the CSV file is saved\File Name.csv'] df.to_json [r'Path where the new JSON file will be stored\New File Name.json']8
Code:
Python3
Làm thế nào để chuyển đổi tất cả các cột CSV thành văn bản?
Đối với điều này, chúng tôi không cần nhập bất kỳ thư viện nào.
import pandas as pd df = pd.read_csv [r'C:\Users\Ron\Desktop\Test\Products.csv'] df.to_json [r'C:\Users\Ron\Desktop\Test\New_Products.json']1
data.append[tuple[next[reader]]]
3 {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":120,"3":1200}}
9Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 2000
import pandas as pd df = pd.read_csv [r'C:\Users\Ron\Desktop\Test\Products.csv'] df.to_json [r'C:\Users\Ron\Desktop\Test\New_Products.json']6
Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 20022
[['ID', 'Value'], [1, 'a'], [2, 'b'], [3, 'c']]
8Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 2005
Output:
FRUIT_NAME PRICE Apple 100 Banana 70 JackFruit 30 Orange 120 Pineapple 90 Guava 50 Grapes 80 Mango 200