Leggi colonne specifiche da un file CSV con modulo CSV?

Python functions

Sto cercando di analizzare un file CSV ed estrarre i dati solo da colonne specifiche.

Esempio CSV:

< codice>ID | Nome | Indirizzo | Città | Stato | CAP | Telefono | OPEID | IPEDS | 10 | C... | 130 W.. | Lu.. | AL... | 3.. | 334.. | 01023 | 10063 | 

Sto cercando di acquisire solo colonne specifiche, ad esempio ID, Nome, Zip > e Telefono.

Il codice che ho esaminato mi ha portato a credere di poter chiamare la colonna specifica con il numero corrispondente, quindi: Nome corrisponderebbe a 2 e l'iterazione di ogni riga utilizzando row[2] produrrebbe tutti gli elementi nella colonna 2. Solo che non è così.

Ecco cosa ho fatto finora:

import sys, argparse, csv da settings import * # command arguments parser = argparse.ArgumentParser(description="csv to postgres", fromfile_prefix_chars ="@" ) parser.add_argument("file", help="file csv da importare", azione ="store") args = parser.parse_args() csv_file = args.file # apre il file csv con open(csv_file, "rb") come csvfile: # ottiene il numero di colonne per la riga in csvfile.readlines(): array = line .split(",") first_item = array[0] num_columns = len(array) csvfile.seek(0) reader = csv.reader(csvfile, delimiter=" ") includes_cols = [1, 2, 6, 7] per riga nel lettore: content = list(row[i] for i in includes_cols) print content 

e mi aspetto che questo stamperà solo le colonne specifiche che voglio per ogni riga tranne non lo fa, ottengo solo l'ultima colonna.

Shop

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically