【发布时间】:2020-12-07 09:51:02
【问题描述】:
对不起,我是菜鸟。 我有一个这样的 csv 文件
customerID , gender , ...
5575-GNVDE , Female , ...
9763-GRSKD , Male , ...
我想把“customerID”列放在列表中
例如:
print(customerID)
喜欢
[5575-GNVDE, 9763-GRSKD , ...]
我已经写好了代码
csvFile = open("WA_Fn-UseC_-Telco-Customer-Churn.csv", "r")
reader = csv.reader(csvFile)
# create list
customerID = []
for item in reader:
# ignore first line
if reader.line_num == 1:
continue
customerID += item[0]
print(customerID)
csvFile.close()
它是这样显示的
['5', '5', '7', '5', '-', 'G', 'N', 'V', 'D', 'E','9', '7', '6', '3', '-', 'G', 'R', 'S', 'K', 'D',...]
我已经读过了:
- Create a csv file with python and put in nested list to it
- Read txt file and put in list with python
- How do I read and write CSV files with Python?
- Reading rows from a CSV file in Python
- Python import csv to list
- How do I read and write CSV files with Python?
请帮助我。 谢谢。
【问题讨论】:
-
使用
pandas库。方法是pandas.read_csv