“lecteur Python 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

Importer un fichier CSV dans Python

import pandas as pd

df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
print (df)
Yawning Yacare

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

lecteur Python CSV

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
Delightful Dormouse

CSV Lire Python

# importing Pandas library
import pandas as pd
 
pd.read_csv(filepath_or_buffer = "pokemon.csv")
 
# makes the passed rows header
pd.read_csv("pokemon.csv", header =[1, 2])
 
# make the passed column as index instead of 0, 1, 2, 3....
pd.read_csv("pokemon.csv", index_col ='Type')
 
# uses passed cols only for data frame
pd.read_csv("pokemon.csv", usecols =["Type"])
 
# returns pandas series if there is only one column
pd.read_csv("pokemon.csv", usecols =["Type"],
                              squeeze = True)
                               
# skips the passed rows in new series
pd.read_csv("pokemon.csv",
            skiprows = [1, 2, 3, 4])
Altaf Sammad Jogilkar

Réponses similaires à “lecteur Python CSV”

Questions similaires à “lecteur Python CSV”

Plus de réponses similaires à “lecteur Python CSV” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code