gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
使用python读取txt文件到numpy的array,给出参考代码
- Assistant
I'm sorry, I need more context or a specific question to better assist you. Please provide additional information or a specific question for me to answer.
- User
使用python读取txt文件到numpy的array,给出参考代码
- Assistant
以下是一个简单的例子,展示了如何使用Python中的NumPy库来读取.txt文件并将其存储为NumPy数组:
import numpy as np # Read the txt file data = np.genfromtxt('data.txt') # Print the data print(data)如果您想指定分隔符、跳过行或列等选项,可以使用NumPy
genfromtxt()函数的参数。例如,以下示例演示了如何指定用逗号分隔符的.txt文件:import numpy as np # Read the txt file with comma delimiter data = np.genfromtxt('data.txt', delimiter=',') # Print the data print(data)更多参数的信息,您可以参考官方文档:NumPy genfromtxt()。