列表的问题…

发布于 2025-01-29 04:11:19 字数 1089 浏览 0 评论 0原文

我是Python的新手,我需要访问文件夹中的CSV文件。这不是问题:

open('c:\ users \ Directory \ Companies.csv')

我尝试了以下操作:

from csv import reader
# read csv file as a list of lists
with open('companies.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Pass reader object to list() to get a list of lists
list_of_rows = list(csv_reader)
print(list_of_rows)

输出

[['Company', 'Departments', 'Employess'],
['A', 'Marketing', '200'],
['B', 'Sales', '300’],
['C', 'HR', '15'],
['D', 'Logistics', '500'],
[‘A’, ‘Admin’, ‘15’],
[‘A’, ‘Research’, ‘30’],
[‘B’, ‘COffice’, ‘150’],
[‘C’, ‘Transporte, ‘200’],
[‘B’, ‘IT’, ‘200’],
[‘B’, ‘Gorg’, ‘12’]]

我无法获得公司“ B”部门总数和包括部门数量的新列表在拥有20多名员工的公司A中。我尝试了一下:

new_list = [x for x in companies if Company = A' and 'Departments' > 20]

def total_elements(companies):
    count = 0
    for element in companies:
        count += 1
    return count

print("the total number is: ", total_elements(new_list))

预先感谢!

I am new in Python and I need to access a csv file that I have in a folder. This is not a problem:

open(‘C:\Users\Directory\companies.csv’)

I have tried this:

from csv import reader
# read csv file as a list of lists
with open('companies.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Pass reader object to list() to get a list of lists
list_of_rows = list(csv_reader)
print(list_of_rows)

OUTPUT

[['Company', 'Departments', 'Employess'],
['A', 'Marketing', '200'],
['B', 'Sales', '300’],
['C', 'HR', '15'],
['D', 'Logistics', '500'],
[‘A’, ‘Admin’, ‘15’],
[‘A’, ‘Research’, ‘30’],
[‘B’, ‘COffice’, ‘150’],
[‘C’, ‘Transporte, ‘200’],
[‘B’, ‘IT’, ‘200’],
[‘B’, ‘Gorg’, ‘12’]]

I am unable to obtain total number of Company ‘B’ departments and a new list that includes the number of departments in company A with more than 20 employees. I tried this:

new_list = [x for x in companies if Company = A' and 'Departments' > 20]

def total_elements(companies):
    count = 0
    for element in companies:
        count += 1
    return count

print("the total number is: ", total_elements(new_list))

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文