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.

1
  • use pandas library. the method is pandas.read_csv Commented Dec 7, 2020 at 9:55

1 Answer 1

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]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.