Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从txt输入到列表的数据-Python_Python_List - Fatal编程技术网

从txt输入到列表的数据-Python

从txt输入到列表的数据-Python,python,list,Python,List,我有文本文件中的数据: 2,58 1,23 0,14 6,58 4,2 1,3 我想让列表中的文本文件中的数据以以下格式写入: [[2, 58, 1, 23, 0, 14] [6, 58, 4, 2, 1, 3]] 我试过这个: folder = open('text.txt', encoding = 'utf-8') data = [numbers.strip().replace(',',' ').split(' ') for numbers in folder] folder.clo

我有文本文件中的数据:

2,58 1,23 0,14
6,58 4,2  1,3
我想让列表中的文本文件中的数据以以下格式写入:

[[2, 58, 1, 23, 0, 14]
 [6, 58, 4, 2, 1, 3]]
我试过这个:

folder = open('text.txt', encoding = 'utf-8')
data = [numbers.strip().replace(',',' ').split(' ') for numbers in folder]
folder.close
print(data)
但我收到的结果是这样的:
['2','58','1','23','0','14']['6','58','4','2','1','3']

如果我试图在列表中的许多位置将
int()
设置为数字,我会收到以下错误:int()参数必须是字符串、类似字节的对象或数字,而不是
list

所以我只需要将列表中的所有字符串从
str
更改为
int
,您能帮我吗?

尝试使用函数它有两个参数,一个是函数、条件等,第二个是列表、集合等可编辑对象,所以您的问题类似于
map(int,yourlist)
并在像
list(map(int,yourlist))一样将其强制转换为列表之后。
有关更多信息,请参阅了解为什么要强制转换它

例子
list(map(int,numbers.strip().replace(',','').split('')
folder.close()
如果你不介意的话,
pd.read_csv(
@Chris Rands这能回答你的问题吗?
folder = open('text.txt', encoding = 'utf-8')
data = [list(map(int, numbers.strip().replace(',',' ').split(' '))) for numbers in folder]
folder.close()
print(data)