Excel数组到2D python lis

2024-04-19 05:27:25 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我必须用Python创建一个列表,使用两个excel电子表格(显示姓名、地点、薪水等的列表)作为模拟公司员工信息列表。我需要把它变成一个2dpython列表。你知道吗

我相信我的代码是正确的,但我不断地指出一个错误,因为'x=+1'。我错过什么了吗?你知道吗

def location_dict():
    f2 = open("Hw#5_Locations.csv", "r")
    dict1 = {}
    for b in f2:
        c = b.split(",")
        dict1[c[0]] = c[1]
        print("dict1", dict1)
    f2.close()
    return dict1

f1 = open("Hw#5_Employees.csv", "r")
x = 0
salary = []
ID_Salary = []
dict2 = location_dict()
for aline in f1:
    aline = aline.replace('\n','')
    values = aline.split(",")
    print(values[0], values[5])

    salary.append([values[0],values[1],values[2],values[5],values[7]])

    if x > 0:
        print("Salary[",x,"]", salary[x][0], salary[x][1], dict2[salary[x]    [2]])

    print("======= end line:", x)

    x =+ 1

提前谢谢!你知道吗


Tags: csvin列表forlocationopendictf2
1条回答
网友
1楼 · 发布于 2024-04-19 05:27:25

使用^{}模块。默认值csv.reader将以Excel方言读取CSV文件:

import csv

with open('Hw#5_Employees.csv', 'r') as f:
    c = csv.reader(f)
    data = list(c)

# do something with data

还可以read the data into a dictionary structure,这样就可以按名称访问列。你知道吗

相关问题 更多 >