“Csv.” Réponses codées

Python Lire CSV

# pip install pandas 
import pandas as pd

# Read the csv file
data = pd.read_csv('data.csv')

# Print it out if you want
print(data)
Random boi

CSV Python écrivez

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
Arrogant Addax

Python CSV Dict Reader

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Tired Turkey

lecteur Python CSV

with open(r'c:\dl\FrameRecentSessions.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
            print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1
    print(f'Processed {line_count} lines.')
riffrazor

Csv.

01
02
03
04
05
06
07
08
09
10
# importing DictReader class  from csv module
from csv import DictReader
 
# opening csv file named as airtravel.csv
with open('airtravel.csv','r') as file:
    reader = DictReader(file)
    print("Data:")
    # printing each row of table as dictionary 
    for row in reader:
        print(row)
Shy Skunk

Réponses similaires à “Csv.”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code