import csv
portfolio = csv.reader(open("portfolio.csv", "rb"))
names = []
for data in portfolio:
names.append(data[0])
print names
but the output was an empty list ([]) :-(
Then I found the type of portfolio object using
print type(portfolio)
that the type is '_csv.reader', then I changed my program to the following and got the list :-)
import csv
portfolio = csv.reader(open("portfolio.csv", "rb"))
portfolio_list = []
portfolio_list.extend(portfolio)
names = []
for data in portfolio_list:
names.append(data[0])
print names
If you have any better idea, please share.
To know more about csv module, http://docs.python.org/lib/module-csv.html
Замечания:
1. Try
portfolio = csv.reader(open("portfolio.csv", "rU"))
It should work
2.
Reading a particular column from a csv file is really useful and worthful. In my project, I also need to write an array of elements into a new column in the existing csv file in Python. Can you please post some information for writing column data into csv file in Python.
Answer
import csv
list = [ x[0] for x in csv.reader(open('x.csv','r')) ]
Взято отсюда
================================
еще полезные ссылки
Серый форум - разработка скриптов
Какой-то форум
Комментариев нет:
Отправить комментарий