用python从文件中读取整数

用python从文件中读取整数,python,Python,我正在读取一个包含100000个数字(每行1个)的文件,使用: 这将返回一个字符串数组,但我需要将它们转换为整数。我尝试使用: var_array += int(line.strip().split('\n')) 但它抛出错误“TypeError:int()参数必须是字符串或数字,而不是“list” 我还尝试使用地图: var_array.append(map(int,line.strip().split('\n'))) 但打印显示了这一点(88>,…) 感谢您的建议。问题出在拆分('\n'

我正在读取一个包含100000个数字(每行1个)的文件,使用:

这将返回一个字符串数组,但我需要将它们转换为整数。我尝试使用:

var_array += int(line.strip().split('\n'))
但它抛出错误“TypeError:int()参数必须是字符串或数字,而不是“list”

我还尝试使用地图:

var_array.append(map(int,line.strip().split('\n')))
但打印显示了这一点(88>,…)


感谢您的建议。

问题出在
拆分('\n')
函数调用中,该函数调用将行转换为由
\n
字符分隔的令牌列表(由于您正在执行
strip()
),因此该行中不应存在该符号)。删除此调用并用int()换行.strip()调用,一切都应该正常

更新版本如下:

var_array = []

with open("IntegerArray.txt", "r") as my_file:
    for line in my_file:
        var_array.append(int(line.strip()))

print(var_array)

问题出在
split('\n')
函数调用中,该函数调用将行转换为由
\n
字符分隔的令牌列表(由于您正在执行
strip()
,所以该符号不应存在于行中)。删除此调用并用int()换行.strip()调用,一切都应该正常

更新版本如下:

var_array = []

with open("IntegerArray.txt", "r") as my_file:
    for line in my_file:
        var_array.append(int(line.strip()))

print(var_array)

您不需要在此处拆分:

var_array.append(int(line.strip()))
剥离和拆分:

>>> "hello,world".split(',')    #split the string on delimiter 
['hello', 'world']
>>> "   hello,world  \r\n".strip()  #removes whitespace from the both end
'hello,world'

您不需要在此处拆分:

var_array.append(int(line.strip()))
剥离和拆分:

>>> "hello,world".split(',')    #split the string on delimiter 
['hello', 'world']
>>> "   hello,world  \r\n".strip()  #removes whitespace from the both end
'hello,world'

如果已安装Pandas,则更易于使用:

>>> import pandas as pd
>>> pd.read_csv('IntegerArray.txt', header=None)
   0
0  1
1  3
2  4
3  5
如果希望将其作为内置数据类型:

>>> list(pd.read_csv('IntegerArray.txt', header=None).values[:,0])
[1, 3, 4, 5]

如果已安装Pandas,则更易于使用:

>>> import pandas as pd
>>> pd.read_csv('IntegerArray.txt', header=None)
   0
0  1
1  3
2  4
3  5
如果希望将其作为内置数据类型:

>>> list(pd.read_csv('IntegerArray.txt', header=None).values[:,0])
[1, 3, 4, 5]

您需要在分隔符而不是换行符上拆分:

var_array += map(int, line.strip().split())
如果它们之间用空格分隔,则只需拆分,如果每个数字之间用
分隔,
则使用
拆分(“,”

如果它们都在单独的行上,只需使用列表comp并强制转换为int:

with open("IntegerArray.txt") as my_file:
        var_array = [int(line.rstrip()) for line in my_file]

您需要在分隔符而不是换行符上拆分:

var_array += map(int, line.strip().split())
如果它们之间用空格分隔,则只需拆分,如果每个数字之间用
分隔,
则使用
拆分(“,”

如果它们都在单独的行上,只需使用列表comp并强制转换为int:

with open("IntegerArray.txt") as my_file:
        var_array = [int(line.rstrip()) for line in my_file]

文件的第一个数字是(540441410879294)。当我删除.split('\n')->(var_array+=line.strip())时,返回('2','6','3','7','5','1','8','0','1','6','3','8','3','9','7','0','8','9','4','6','4','2','4','0','3','6','5',…)。是的,每行有一个数字,我只是用逗号列出了它们。而不是var_数组+=do var_数组。append(line.strip())var_数组。append(line.strip())也将数组作为字符串数组返回。对不起,请按照anwer:var_数组中的方式执行。append(int(line.strip())文件的第一个数字是(540441410879294)。当我删除.split('\n')->(var_array+=line.strip())时,返回('2','6','3','7','5','1','8','0','1','6','3','8','3','9','7','0','8','9','4','6','4','2','4','0','3','6','5',…)。是的,每行有一个数字,我只是用逗号列出了它们,而不是var_数组+=do var_数组。append(line.strip())var_数组。append(line.strip())也将数组作为字符串数组返回。对不起,请按照anwer:var_数组中的方式执行。append(int(line.strip())