7

Hello I have a database that i am trying to make a .csv file quickly from.

my data looks like this.

Song_Name,File_Name,Artist_Name,Artist_ID
Song1,filename1,artistname,artist001
Song1,filename1,artistname,artist001
Song1,filename1,artistname,artist001
Song1,filename1,artistname,artist001

and this is how I need it to look.

Song_Name,,File_Name,Artist_Name,,Artist_ID
Song1,,filename1,artistname,,artist001
Song1,,filename1,artistname,,artist001
Song1,,filename1,artistname,,artist001
Song1,,filename1,artistname,,artist001

what would be the best way to do this. thank you.

2
  • just insert into each row by index and write Commented Oct 29, 2014 at 2:01
  • i was playing around with import csv with open('x.csv','r') as csvinput: with open('output.csv', 'w') as csvoutput: writer = csv.writer(csvoutput) for row in csv.reader(csvinput): writer.writerow(row+['Blank']) Commented Oct 29, 2014 at 2:41

3 Answers 3

5

You can insert blank "columns" in a CSV file simply by writing None or an empty string ''.

For example:

with open('songs.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(
        ['Song_Name', None, 'File_Name', 'Artist_Name', None, 'Artist_ID']
    )  # Write headers
    # Now you iterate over your data:
    for row in data:
        writer.writerow([row['song_name'], None, row['file_name'], ...])

Your CSV file will correctly include the extra commas as required for your blank columns, including a trailing comma if required.

If you use a DictWriter, then it's even easier. You simply don't populate the dictionary with the keys you want to leave out:

with open('songs.csv', 'w', newline='') as f:
    headers = ['Song_Name', None, 'File_Name', ...]
    writer = csv.DictWriter(f, fieldnames=headers)
    writer.writeheader()
    # Now write a sample row:
    row = {'Song_Name': 'Dumb', 'Artist_Name': 'Nirvana'}
    writer.writerow(row)  # Automatically skips missing keys
Sign up to request clarification or add additional context in comments.

Comments

2

For future readers, I post an alternative to do it with Pandas, if the csv is readable with this module (like in the original question).

Using Pandas with its alias pd, first we read the data with pd.read_csv (specify the delimiter sep = ','). Then, we create a DataFrame (df) containing only one empty column. We insert this column in the first DataFrame in the position that we want. Then, we save the data in the csv again using df.to_csv. Let's see this in code, for a csv file called test.csv:

import pandas as pd

# Read the file.
df = pd.read_csv('test.csv', header = None) 

# Create single (empty) column dataframe with the same number of rows as the original.
empty_col = pd.DataFrame(['']*len(df)) 

# Insert in original dataframe
df.insert(1, 'col1', empty_col) 
df.insert(4, 'col2', empty_col) 

# Save to csv
pd.to_csv('test.csv', index = False, header = False)

Then, we obtain the following in the file test.csv :

Song_Name,,File_Name,Artist_Name,,Artist_ID
Song1,,filename1,artistname,,artist001
Song1,,filename1,artistname,,artist001
Song1,,filename1,artistname,,artist001
Song1,,filename1,artistname,artist001

Note that I chose header = None to avoid that the first line is taken as headers. I do this because the original question asks needs two columns completely empty (including headers) and a dataframe cannot have two columns with the same name. In our case the names that we give to the columns ('col1', 'col2') do not matter, since we are not going to save them in the file: we specify header = False when saving the csv.

Comments

-2

Here is my answer to help you out.

Firstly, I would suggest using Pandas in the IPython environment rather than Python's built-in CSV reader. Pandas provides some powerful stuff for munging tabular data. That said, here's what you can do using Python's built-in CSV module.

with open('data.csv', 'r') as infile:
    with open('data_out.csv', 'w') as outfile:
        for line in csv.reader(infile):
            newline = []
            for element in line:
                if line.index(element) in [1, 3]: # crucial part here: identify where you want to make insertions
                    newline.append(' ')
                newline.append(element)
            print(newline)
            csv.writer(outfile).writerow(newline)

As an evaluation between whether to go Pandas vs. simply iterating over the file, it sorta depends - from my own experience, I have found considerable memory overhead by loading a large CSV file into Pandas, so I switched to processing my data files using Python's built-in modules instead. That said, I might not have mastered Pandas deeply enough just yet. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.