CSV Data Converter - Python Programming Exercise

In this exercise, you will develop a Python program to read a CSV file containing comma-separated values. This exercise is perfect for practicing file handling, string manipulation, and loops in Python. By implementing this program, you will gain hands-on experience in handling file operations, string manipulation, and loops in Python. This exercise not only reinforces your understanding of file handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Managing Files

 Exercise

CSV Data Converter

 Objective

Develop Python program to read a CSV file containing comma-separated values. The file should have four data blocks per line (three text and one numeric), and the program will convert it into a text file. Each entry in the output file should have the corresponding data on separate lines. For instance, if the input file contains:

"John", "López Pérez", "Alicante", 25
"Antonio", "Pérez López", "Madrid", 27

The program should output:

John
López Pérez
Alicante
25
Antonio
Pérez López
Madrid
27

 Example Python Exercise

 Copy Python Code
import csv

# Python program to read a CSV file and convert it into a text file

def convert_csv_to_txt(input_file, output_file):
    try:
        # Open the CSV file for reading
        with open(input_file, 'r') as csvfile:
            # Create a CSV reader object
            csvreader = csv.reader(csvfile)
            
            # Open the output text file for writing
            with open(output_file, 'w') as txtfile:
                for row in csvreader:
                    # Write each element of the row to the text file on a new line
                    for entry in row:
                        txtfile.write(entry.strip() + '\n')
                    
        print(f"Conversion successful! Data has been written to {output_file}.")
    
    except FileNotFoundError:
        print(f"Error: The file '{input_file}' does not exist.")
    except Exception as e:
        print(f"Error: {str(e)}")

# Example usage
convert_csv_to_txt('input.csv', 'output.txt')

 Output

Example Input (CSV file: input.csv):

"John", "López Pérez", "Alicante", 25
"Antonio", "Pérez López", "Madrid", 27

Example Output (Text file: output.txt):

John
López Pérez
Alicante
25
Antonio
Pérez López
Madrid
27

 Share this Python Exercise

 More Python Programming Exercises of Managing Files

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  File content comparer

    In this exercise, you will develop a Python program to compare two files (of any type) and determine if they are identical (i.e., have the same content). This exer...

  •  Show BMP on console

    In this exercise, you will develop a Python program to decode and display a Netpbm image file. This exercise is perfect for practicing file handling, string ma...

  •  Extract Text Information from a Binary File

    In this exercise, you will develop a Python program to extract only the alphabetic characters contained in a binary file and dump them into a separate file. This e...

  •  Dump

    In this exercise, you will develop a Python program to create a "dump" utility: a hex viewer that displays the contents of a file, with 16 bytes in each row and 24 ro...

  •  Text Filter

    In this exercise, you will develop a Python program to create a utility that censors text files. This exercise is perfect for practicing file handling, string ...

  •  SQL to Plain Text

    In this exercise, you will develop a Python program to parse SQL INSERT commands and extract their data into separate lines of text. This exercise is perfect f...