Read Python csv and export to txt

data2.csv


Nafn,Sysla,ID,Starfsgrein,Aldur,Kynlíf
Dom,Árnessýsla,00027861s,Embættismaður,30,karlkyns
Ola,Gullbringusýsla,00033014s,bankastjóri,26,kona
Qiv,Vestur-Skaftafellssýsla,00087295j,Sjómaður,47,karlkyns
Uba,Suður-Múlasýsla,00043765e,Bóndi,66,kona
Yhe,Norður-Múlasýsla ,00021559e,Fréttaritari,35,kona

・ Csv module is a standard library -Specify the relative path and mode of the read / write file -List the keys and values connected by = and join with a semicolon.

import csv


def get_data_csv(path, mode):
    list = []
    with open(path, mode) as f:
        dict_data = csv.DictReader(f)
        for dict in dict_data:
            list.append(dict)
    return list


if __name__ == "__main__":
    read_path = 'data2.csv'
    read_mode = "r"
    write_path = 'write_data2.txt'
    write_mode = "w"
    list = get_data_csv(read_path, read_mode)
    with open(write_path, write_mode) as f:
        for dict in list:
            str = ";".join([key + '=' + value for key, value in dict.items()])
            f.write(str)
            f.write('\n')

write_data2.txt


Nafn=Dom;Sysla=Árnessýsla;ID=00027861s;Starfsgrein=Embættismaður;Aldur=30;Kynlíf=karlkyns
Nafn=Ola;Sysla=Gullbringusýsla;ID=00033014s;Starfsgrein=bankastjóri;Aldur=26;Kynlíf=kona
Nafn=Qiv;Sysla=Vestur-Skaftafellssýsla;ID=00087295j;Starfsgrein=Sjómaður;Aldur=47;Kynlíf=karlkyns
Nafn=Uba;Sysla=Suður-Múlasýsla;ID=00043765e;Starfsgrein=Bóndi;Aldur=66;Kynlíf=kona
Nafn=Yhe;Sysla=Norður-Múlasýsla ;ID=00021559e;Starfsgrein=Fréttaritari;Aldur=35;Kynlíf=kona

Reference URL

https://note.nkmk.me/python-csv-reader-writer/

Recommended Posts

Read Python csv and export to txt
Read Python csv file
How to read a CSV file with Python 2/3
Scraping tabelog with python and outputting to CSV
Read JSON with Python and output as CSV
[Python] How to read data from CIFAR-10 and CIFAR-100
Read CSV file with Python and convert it to DataFrame as it is
Read csv with python pandas
Write to csv with Python
How to read csv containing only integers in Python
[Python] Read the csv file and display the figure with matplotlib
[R] [Python] Memo to read multiple csv files in multiple zip files
[Python] Use this to read and write wav files [wavio]
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
Python 3.6 on Windows ... and to Xamarin.
[Introduction to Python3 Day 1] Programming and Python
[Python] Write to csv file with Python
Output to csv file with Python
Read and use Python files from Python
Python CSV file reading and writing
Export and output files in Python
Python logging and dump to json
Reading and writing CSV with Python
Selenium and python to open google
Create and read messagepacks in Python
How to import CSV and TSV files into SQLite with Python
Read big endian binary in Python and convert it to ndarray
If you want to assign csv export to a variable in python
[Python] How to convert db file to csv
How to package and distribute Python scripts
From Python to using MeCab (and CaboCha)
Tips to make Python here-documents easier to read
How to read pydoc on python interpreter
Read CSV file with python (Download & parse CSV file)
How to install and use pandas_datareader [Python]
Bind methods to Python classes and instances
[Python] Convert csv file delimiters to tab delimiters
Fractal to make and play with Python
Read and write csv files with numpy
Porting and modifying doublet-solver from python2 to python3.
Read and write JSON files in Python
python: How to use locals () and globals ()
[Part1] Scraping with Python → Organize to csv!
Python> Output numbers from 1 to 100, 501 to 600> For csv
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
Compress python data and write to sqlite
How to read CSV files in Pandas
[python] Read html file and practice scraping
How to use is and == in Python
Read CSV and analyze with Pandas and Seaborn
Read and format a csv file mixed with comma tabs with Python pandas
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
[Python] How to read a csv file (read_csv method of pandas module)
[Markov chain] I tried to read quotes and negative emotions into Python.
Various ways to read the last line of a csv file in Python
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
Updated to Python 2.7.9
MessagePack-Try to link Java and Python with RPC
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Image characters and post to slack (python slackbot)