列表的问题…
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论