0

Sorry, I'm a noob. I have a csv file like this

customerID , gender , ...
5575-GNVDE , Female , ...
9763-GRSKD , Male   , ...

I want put "customerID" column in list

ex:

print(customerID)

like

[5575-GNVDE, 9763-GRSKD , ...]

I already write the code

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()

it show like this

['5', '5', '7', '5', '-', 'G', 'N', 'V', 'D', 'E','9', '7', '6', '3', '-', 'G', 'R', 'S', 'K', 'D',...]

I already read:

Please help me. Thank you.

Alex
  • 6,610
  • 3
  • 20
  • 38
xeroxion
  • 15
  • 5

1 Answers1

2

You are using this:

    customerID += item[0]

But that doesn't do what you think it does. customerID is a list and you're using the add operator on it, so Python tries to interpret item[0] as a list as well, which is a str, but can be considered a list of characters - so that's exactly what gets added.

Instead, use:

    customerID.append(item[0])

Or, if you prefer:

    customerID += [item[0]]
Grismar
  • 27,561
  • 4
  • 31
  • 54