Python Language
Lettura e scrittura CSV
Ricerca…
Scrivere un file TSV
Pitone
import csv
with open('/tmp/output.tsv', 'wt') as out_file:
tsv_writer = csv.writer(out_file, delimiter='\t')
tsv_writer.writerow(['name', 'field'])
tsv_writer.writerow(['Dijkstra', 'Computer Science'])
tsv_writer.writerow(['Shelah', 'Math'])
tsv_writer.writerow(['Aumann', 'Economic Sciences'])
File di uscita
$ cat /tmp/output.tsv
name field
Dijkstra Computer Science
Shelah Math
Aumann Economic Sciences
Usando i panda
Scrivi un file CSV da un dict
o da un DataFrame
.
import pandas as pd
d = {'a': (1, 101), 'b': (2, 202), 'c': (3, 303)}
pd.DataFrame.from_dict(d, orient="index")
df.to_csv("data.csv")
Leggi un file CSV come DataFrame
e convertilo in un dict
:
df = pd.read_csv("data.csv")
d = df.to_dict()
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow