0

Hey so i have a program here that reads into a CSV file which i created earlier

import csv
import psycopg2
with open('C:\\Users\\Danis Porovic\\PycharmProjects\\Module1\\berichten.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    for line in csv_reader:
        print(line)

the for loop gives this as a result

['vies', 'Mon 07 Nov 2022 18:43', 'Mia', 'Haarlem']
['vies', 'Mon 07 Nov 2022 18:43', 'Mia', 'Amsterdam']
['vies', 'Mon 07 Nov 2022 18:43', 'Mia', 'Sittard']
['vies', 'Mon 07 Nov 2022 18:43', 'Mia', 'Venlo']
['vies', 'Mon 07 Nov 2022 18:43', 'Mia', 'Helmond']
['Het zou wel wat schoner mogen zijn', 'Tue 08 Nov 2022 00:49', 'Tijmen', 'Hilversum']
['Het zou wel wat schoner mogen zijn', 'Tue 08 Nov 2022 00:49', 'anoniem', 'Roosendaal']

Now how could i save all this information from the for loop in a single variable?

4
  • Create a new list before you start the loop, and append the line to the list on each iteration. Commented Nov 8, 2022 at 1:11
  • You create a list and append each row to a list. Commented Nov 8, 2022 at 1:11
  • 2
    lines = list(csv.reader(csv_file)). No for loop required and this will give you a list of rows. Commented Nov 8, 2022 at 1:39
  • lines = list(csv.reader(csv_file)) is definitely the way to go. Solid answer @Mark Tolonen Commented Nov 8, 2022 at 1:43

1 Answer 1

1

Something like this should do the trick:

result = []
for line in csv_reader:
    result.append(line)

Or using list comprehension:

result = [line for line in csv_reader]

Edit: After comments outlining in more detail what you're looking for. We can transpose the list you usually get so each sublist represents a column from the .csv rather than a row:

import csv
with open('a.csv', 'r') as csv_file:
    regular_list = list(csv.reader(csv_file))
    transposed_result = list(map(list, zip(*regular_list)))
    print(transposed_result)

Shoutout to @Mark Tolonen for removing the for loop iteration and jumping right to csv.reader(csv_file)

Sign up to request clarification or add additional context in comments.

9 Comments

The method above did display all the information but not in the way i want it to. I would like to have each line as a list. This so i can use indexing on it later.
We currently have a list of lists, where the sublist has many strings. Were you still hoping for a list of lists, but in a different format? Say... the first list is ['vies', 'vies', etc.]? Or did you want a single list, where we've concatenated the sublist into single strings? What did you have in mind in particular?
The CSV files is structured in this way: 'Message', Date and Time', 'Name', 'Station' I would like to save the information from the for loop in a variable so that i can use indexing on the list later on and grab all the messages for instance. So for every line create a list with 4 strings in it
I'm not sure I follow. We currently have list, where each item is a list with 4 strings which allows for indexing. When you say 'variable' do you mean dictionary? If so that'd allow you to look up values e.g. result[0]["Message"] would output "vies". Right not result[0][0] outputs "vies"
I would like to have a variable contain the exact information which i gathered with the for loop. So that with index[0] i can get all the messages, with index[1] all the dates, with index[2] all the names etc.
|

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.