python - 如何在 Collaboratory 中将 tsv 读入 int np 数组
问题描述
我有一个文本文件,由
1 0 0 1
1 1 1 1
1 1 1 0
1 0 1 0
0 0 0 1
. 我需要它作为整数数组
np.array([
[1, 0, 0, 1]
[1, 1, 1, 1]
[1, 1, 1, 0]
[1, 0, 1, 0]
[0, 0, 0, 1]
])
在 Google 协作中。
如果我做
from google.colab import files
import pandas as pd
import numpy as np
uploaded = files.upload()
data = pd.read_table('filename.txt')
np_arr = np.array(data)
,我得到
[
['1 0 0 1']
['1 1 1 1']
['1 1 1 0']
['1 0 1 0']
['0 0 0 1']
]
.
我不能dtype=int
用来将这些字符串更改为整数。有人知道该怎么做吗?此外,我过去在没有说明实际文件名的情况下这样做,只是使用了一个虚拟名称,因此可以在不更改代码的情况下读取新文件(在 Google Collab 中)。有人知道这种可能性吗?
解决方案
推荐阅读
- node.js - Oauth 2 (Passport.js) 登录应该永远不会失败,对吧?
- angular - 标头上的 Set-Cookie 尚未设置 Cookie
- python - 在kivy中设置动态类
- typescript - 转换接口,使得一个类型的出现被另一个类型的打字稿替换
- python - 在一列中组合两种数据类型
- reactjs - 为什么 state 属性在 return 之前启动?
- python - folium 可以在 anaconda 中运行吗?
- mysql - 多行数据作为单个查询结果 - My SQL
- react-native - 正如我已切换到 react-navigation-drawer 但遇到错误不知道为什么
- java - Hibernate - 在资源中找不到 cfg.xml