【问题标题】:How do I convert items in a .csv to a list in python如何将 .csv 中的项目转换为 python 中的列表
【发布时间】:2022-01-10 06:54:18
【问题描述】:

我有一个包含行和列变量的 .csv 文件。行是 B1-K1,列是 A2-A7。实际数据是从 B2 到 K7。我希望能够将值放入列表并调用它们,如果有办法以 0,1,2,,3,4,5,0,1 的模式对数据(B2-K7)进行编号.... 那会很好。

import csv
file = open(r'C:/cpps/python project/prodata.csv')
csvreader = csv.reader(file)
header = next(csvreader)
print(header)
rows = []
for row in csvreader:
    rows.append(row)
print(rows)
file.close()

【问题讨论】:

  • 目前尚不清楚您要在这里实现什么。 “行是B1-K1,列是A2-A7”是什么意思?那么“实际数据”怎么可能是“从 B2 到 K7”?您能否提供一些示例数据并解释您要做什么?
  • 它就像一个 .csv 格式的 exel 表。城市/年 2010 2011 .... 2019 是 B1-K1 城市是 A2-A7 城市 A 城市 B 城市 C 城市 D 城市 E 城市 F

标签: python list csv


【解决方案1】:

有很多方法可以给猫剥皮,但如果您只是阅读 .csv(而不是 .xlsx),则不需要 Excel 特定的库:


# this code just (over)writes a .csv for demonstration purposes
from csv import writer
with open('block.csv', 'w', newline='') as f:
    data = [[(r+1)*100 + c+1 for c in range(15)] for r in range(9)]
    cw = writer(f)
    cw.writerows(data)


# this is how you can do it with pandas
import pandas as pd
df = pd.read_csv('block.csv', header=None)
# prints the entire dataframe
print(df)
# this selects the data you want (A2:A7), but it's still a dataframe
print(df.loc[1:6, 0:0])
# this selects the data, and the first columns from it, and then turns the values into a list
print(df.loc[1:6, 0:0][0].values.tolist())
# similarly, this selects B1:K1, turns the values into a list of lists and then selects the first
print(df.loc[0:0, 1:10].values.tolist()[0])


# you can also do it with the standard csv module
from csv import reader
# opening the file for reading
with open('block.csv', 'r') as f:
    cr = reader(f)
    # the reader will read the rows as a list of strings, we want numbers
    data = [[int(x) for x in row] for row in list(cr)]
    # prints the entire list of lists
    print(data)
    # this selects the first element from rows 1 through 6, so A2:A7
    print([x[0] for x in data[1:7]])
    # and from the first row, columns 2 through 11, so B1:K1 
    print(data[0][1:11])


# you don't need the csv module, although I would not recommend this, unless you must and you're
# certain your .csv doesn't have any problems
with open('block.csv', 'r') as f:
    # just reading and splitting the lines and converting the values to integer
    data = [[int(x) for x in line.strip().split(',')] for line in f]
    # the rest of the code is the same as for the csv.reader
    print(data)
    print([x[0] for x in data[1:7]])
    print(data[0][1:11])

作为奖励,这里有一个函数,允许您只使用 Excel 中的范围,以避免计数错误(就像我最初发布答案时所做的那样):

import re

def col_to_index(col):
    return sum((ord(c) - 64) * 26**i for i, c in enumerate(reversed(col))) - 1


def df_excel_range(df, excel_range):
    import re
    coords = [(col_to_index(cell[0]), int(cell[1])-1) for cell in [
        re.match('([A-Z]+)(\d+)', cell).groups() for cell in excel_range.upper().split(':')]]
    if len(coords) == 1:
        coords = [*coords[0], *coords[0]]
    elif len(coords) != 2:
        raise SyntaxError(f'not a valid range {excel_range}')
    return df.loc[coords[0][1]:coords[1][1], coords[0][0]:coords[1][0]]

您可以使用它从数据框中选择一个范围,如下所示:

print(df_excel_range(df, 'A2:A7')[0].values.tolist())

【讨论】:

    【解决方案2】:

    您可以使用pandas 来完成。

    import pandas as pd
    df = pd.read_csv('yourfilename.csv')
    l = df.values.tolist() # this list will contain the whole csv file in a list.
    

    【讨论】:

    • 这是否能够具有特定的参数来将列表指定到某一行。
    猜你喜欢
    • 2017-03-25
    • 2012-07-21
    • 2016-07-11
    • 1970-01-01
    • 2013-08-13
    • 2021-01-28
    • 2022-12-17
    • 2018-10-02
    • 1970-01-01
    相关资源
    最近更新 更多