Python 在 CSV 中添加空白列

6 投票
3 回答
17809 浏览
提问于 2025-04-30 07:26

你好,我有一个数据库,想快速把它做成一个 .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

而我希望它看起来是这样的。

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

请问有什么好的方法可以做到这一点呢?谢谢。

暂无标签

3 个回答

-2

这是我给你的一些建议。

首先,我建议在IPython环境中使用Pandas,而不是Python自带的CSV读取器。Pandas在处理表格数据方面非常强大。不过,下面我会告诉你如何使用Python自带的CSV模块来做这件事。

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)

关于选择使用Pandas还是简单地遍历文件,这其实有点看个人经验。我发现加载一个大的CSV文件到Pandas里会占用很多内存,所以我选择使用Python自带的模块来处理我的数据文件。不过,也许我对Pandas还没有掌握得够深入。:-)

1

为了方便以后的读者,我分享一个用Pandas来处理的方法,前提是这个csv文件可以被这个模块读取(就像原问题中提到的那样)。

使用Pandas,我们通常会用它的别名pd。首先,我们通过pd.read_csv来读取数据(这里要指定分隔符sep = ',')。接着,我们创建一个只包含一个空列的DataFrame(数据框),然后把这个空列插入到我们想要的位置。最后,我们再用df.to_csv把数据保存回csv文件。下面是处理名为test.csv的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)

然后,我们在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

注意,我选择了header = None,这样第一行就不会被当作表头。我这样做是因为原问题需要有两列完全空白(包括表头),而一个数据框不能有两个同名的列。在我们的例子中,给列起的名字('col1','col2')并不重要,因为我们不会把它们保存到文件里:在保存csv时,我们指定header = False

4

你可以在CSV文件中插入空的“列”,只需要写上None或者一个空字符串''就可以了。

比如说:

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'], ...])

这样你的CSV文件就会正确地包含额外的逗号,以便为你的空列留出位置,如果需要的话,最后还会有一个逗号。

如果你使用DictWriter,那就更简单了。你只需要在字典中不填入你想要省略的键就可以了:

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

撰写回答