How can Python convert CSV data into an array?

To convert CSV data into an array, you can utilize Python’s csv module. Below is an example code demonstrating how to read a CSV file and convert it into an array:

import csv

def read_csv_to_array(file_path):

    data = []

    with open(file_path, 'r') as file:

        csv_reader = csv.reader(file)

        for row in csv_reader:

            data.append(row)

    return data

# 用例子.csv替换成你要读取的CSV文件的路径

file_path = '例子.csv'

array_data = read_csv_to_array(file_path)

# 打印数组数据

for row in array_data:

    print(row)

This code defines a read_csv_to_array function that takes a path to a CSV file as input and returns a two-dimensional array containing the CSV data. You can then further process or analyze it as needed.

Please ensure that the Python csv module is installed (usually included by default in Python) and that the specified CSV file exists.

bannerAds