【问题标题】:counting all the rows of appended list python计算附加列表python的所有行
【发布时间】:2022-01-21 15:05:04
【问题描述】:

我正在尝试使用 python 自动填充 mysql 数据库。 我还需要计算行数(在这种情况下应该是 48)。 但无论我只是使用 len() 还是尝试使用 pandas,我总是得到 3 作为 awsner。这只是我没有看到的缩进错误吗? 脚本的其余部分运行良好。

这是完整的代码:(删除了与mysql的连接部分,因为它不需要共享)

for folder in diff_list:
path = os.path.join(nipt_output, folder)
print(path)
os.chdir(path)
files = os.listdir(path)
print(files)
try:
    pool_path = os.path.join(path, "ProcessLogs")
    os.chdir(pool_path)

    print(os.getcwd())
    for file in os.listdir("."):
        if fnmatch.fnmatch(file, '*pool_report*.tab'):
            print(file)
            library = file
    print(library)

    pool = []
    with open(library, 'r') as f:
        next(f)
        reader = csv.reader(f, delimiter='\t')
        for pool in reader:
            pool.append(pool)
    print(pool)
    
    list_pool = []
    index_list = [0, 3, 2]
    index = 0
    for i in index_list:
        list_pool.append(pool[index_list[index]])
        index= index + 1
    print(list_pool)

    
    cursor = cnx.cursor()
    
    number_samples = len(list_pool)

    sql = """INSERT INTO pool_report (batch_name, number_samples, pool_type, pool_barcode) VALUES (%s, %s, %s, %s)"""
    val = (list_pool[0], number_samples, list_pool[1], list_pool[2])
    cursor.execute(sql, val)

这是我得到的输出:

result of the len(list_pool)

应该是 48,但我得到了 3。

【问题讨论】:

  • index_list 有 3 个元素。您在该 for 循环中将三个元素附加到 list_pool。你想要list_pool.extend 代替吗?
  • 当我使用 list_pool.extend 而不是 list_pool.append 时。我收到关于双值的错误
  • 在执行for pool in reader: 时,您将覆盖之前的变量pool = []。您应该更改此名称。

标签: python pandas list


【解决方案1】:

可能是这样的:

for folder in diff_list:
    path = os.path.join(nipt_output, folder)
    print(path)
    os.chdir(path)
    files = os.listdir(path)
    print(files)
    try:
        pool_path = os.path.join(path, "ProcessLogs")
        os.chdir(pool_path)

        print(os.getcwd())
        for file in os.listdir("."):
            if fnmatch.fnmatch(file, '*pool_report*.tab'):
                print(file)
                library = file
        print(library)

        pool = []
        with open(library, 'r') as f:
            next(f)
            reader = csv.reader(f, delimiter='\t')
            for pool in reader:
                pool.append(pool)
        print(pool)
        
        list_pool = []
        index_list = [0, 3, 2]
        index = 0
        for i in index_list:
            list_pool.extend(pool[index_list[index]])
            index= index + 1
        print(list_pool)

        
        cursor = cnx.cursor()
        
        number_samples = len(list_pool)

        sql = """INSERT INTO pool_report (batch_name, number_samples, pool_type, pool_barcode) VALUES (%s, %s, %s, %s)"""
        val = (list_pool[0], number_samples, list_pool[1], list_pool[2])
        cursor.execute(sql, val)

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2019-08-04
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多