0

i have a simply csv file like this:

col1,col2,col3 house,55,600 cottage,88,100

I read in this way:

with open("house.csv", "r+", newline="", encoding="UTF-8") as csv_file:
    file_r = csv.DictReader(csv_file)
    for row in file_r:
        print(row["col1"])

How can I save the result of row["col1"] to a list?

Thanks

I try to save like this:

a = list(row["col1"])
        print(a)

But I obtained

['h', 'o', 'u', 's', 'e']
['c', 'o', 't', 't', 'a', 'g', 'e']

1 Answer 1

1

When you write list(row["col1"]) you are converting the string value in col1 in each row to a list. What you want is to append each value to a list:

col1_values = []

for row in file_r:
    col1_values.append(row["col1"])
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.