Python 如何从列表中的列表中删除撇号

Python 如何从列表中的列表中删除撇号,python,python-3.x,csv,Python,Python 3.x,Csv,我试图从.csv文件中计算行平均值,并返回每行平均值的列表。目前,我可以 in_ = [['1,2'], ['1,1,1,1'], ['-1,0,1'], ['42,17']] 但我需要删除撇号,以平均每个列表!我尝试过使用int(),但没有成功 我的期望输出 out = [[1, 2], [1, 1, 1, 1], [-1, 0, 1], [42, 17]] 我目前的代码是: def line_averages(filename): """takes a file and retu

我试图从
.csv
文件中计算行平均值,并返回每行平均值的列表。目前,我可以

in_ = [['1,2'], ['1,1,1,1'], ['-1,0,1'], ['42,17']]
但我需要删除撇号,以平均每个列表!我尝试过使用
int()
,但没有成功

我的期望输出

out = [[1, 2], [1, 1, 1, 1], [-1, 0, 1], [42, 17]]
我目前的代码是:

def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    f = open(filename, 'r')
    lines = f.readlines()
    #print(lines)
    f.close()
    words = []
    for line in lines:
        words.append(line.split())
    for i in words:
        words.replace("'", "")
    return words

您正在这里重新发明CSV读卡器滚轮。使用来为您处理拆分;然后,我们只需将字符串列转换为整数:

def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    with open(filename, 'r', newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            yield [int(c) for c in row]
for row in line_averages(some_file_name):
    # do something with each row
def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    with open(filename, 'r') as f:
        return [[int(c) for c in line.split(',')] for line in f]
这将生成一个生成器,迭代将为您提供带有整数的行:

def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    with open(filename, 'r', newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            yield [int(c) for c in row]
for row in line_averages(some_file_name):
    # do something with each row
def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    with open(filename, 'r') as f:
        return [[int(c) for c in line.split(',')] for line in f]
您还可以返回列表的列表:

def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    with open(filename, 'r', newline='') as f:
        reader = csv.reader(f)
        return [[int(c) for c in row] for row in reader]
您的尝试在空格上拆分,而不是在逗号上拆分。您必须在
上显式拆分,并将列转换为整数:

def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    with open(filename, 'r', newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            yield [int(c) for c in row]
for row in line_averages(some_file_name):
    # do something with each row
def line_averages(filename):
    """takes a file and returns the average values of each line in a
    .csv file"""
    with open(filename, 'r') as f:
        return [[int(c) for c in line.split(',')] for line in f]
我在这里用来生成列表列表,将文件中的每一行转换为整数列表

我还将该文件用作;这确保无论托管块中发生什么情况,文件都已关闭;不需要手动关闭它


我还将该文件用作迭代器;每次迭代(如
for
循环)都会得到文件中的下一行。无需使用
file.readlines()

预先读取所有行,因为整个列表元素是一个字符串。首先尝试在
上拆分它们,然后通过“删除撇号”强制转换为整数。您的意思是“将字符串转换为整数”吗?这可能更容易搜索。请显示用于从csv文件读取的代码,好吗?可能有一种方法可以自动完成这项工作,而无需繁琐的后期处理步骤。您的问题不是“如何删除撇号”,而是“如何从CSV文件中读取数字”。您的问题设法避免了投票失败和关闭,只是因为您添加了自己的解决方案尝试。这给了我更多的背景来帮助你。以后,请确保您的问题从这些信息开始。感谢您的回答,当使用它输出的确切代码时:@SebTerry:是的,您有一个迭代器。现在,您可以在
for
循环中使用它。