简体   繁体   English

将混合分隔和非分隔数据写入 csv 文件

[英]Writing mixed delimited and not-delimited data to csv file

In this code, some information are written in the csv file.在这段代码中,一些信息写入了 csv 文件。 Specifically, I want to ignore any delimited character (comma, space, ...) for the key string.具体来说,我想忽略key字符串的任何分隔字符(逗号、空格等)。 In other words, I want to see the key in one Excel cell.换句话说,我想在一个 Excel 单元格中查看密钥。 The rest is OK and should be delimited.其余的都可以,应该被分隔。

df = pd.read_csv('test.batch.csv')
print(df)

# Creating a dictionary
data = {'Value':[0,0,0]}
kernel_df = pd.DataFrame(data, index=['M1','M2','M3'])
my_dict = {'dummy':kernel_df}
# dummy  ->          Value
#               M1      0
#               M2      0
#               M3      0

for name, df_group in df.groupby('Name'):
    my_dict[name] = pd.concat(
        [g.reset_index(drop=True) for _, g in df_group.groupby('ID')['Value']],
        axis=1
    )

print(my_dict)

with open('output.csv', 'w') as f:
    for key in my_dict.keys():
        f.write("%s\n"%(key))   <-- This should be written in one cell
        df2 = my_dict[key]
        df2.to_csv(f)

The output is输出是

   ID                  Name Metric  Value
0   0  K1::foo(bar::z(x,u))     M1     10
1   0  K1::foo(bar::z(x,u))     M2      5
2   0  K1::foo(bar::z(x,u))     M3     10
3   1             K2::foo()     M1     20
4   1             K2::foo()     M2     10
5   1             K2::foo()     M3     15
6   2  K1::foo(bar::z(x,u))     M1      2
7   2  K1::foo(bar::z(x,u))     M2      2
8   2  K1::foo(bar::z(x,u))     M3      2
{'dummy':     Value
M1      0
M2      0
M3      0, 'K1::foo(bar::z(x,u))':    Value  Value
0     10      2
1      5      2
2     10      2, 'K2::foo()':    Value
0     20
1     10
2     15}

And the CSV file in Excel looks like Excel 中的 CSV 文件看起来像

在此处输入图片说明

在此处输入图片说明

As you can see the 6th row is split into two columns.如您所见,第 6 行分为两列。 The output file in plain text looks like纯文本输出文件看起来像

dummy
,Value
M1,0
M2,0
M3,0
K1::foo(bar::z(x,u))
,Value,Value
0,10,2
1,5,2
2,10,2
K2::foo()
,Value
0,20
1,10
2,15

How can I fix that?我该如何解决?

You could use the csv module to ensure that special characters in the key will be correctly quoted or escaped.您可以使用 csv 模块来确保密钥中的特殊字符将被正确引用或转义。 As you use the default formattings, ie a comma delimiter and a double quote as string delimiter, you can use the defaults for the csv module.由于您使用默认格式,即逗号分隔符和双引号作为字符串分隔符,您可以使用 csv 模块的默认值。 Your code would become:你的代码会变成:

import csv
...
with open('output.csv', 'w') as f:
    wr = csv.writer(f)
    for key in my_dict.keys():
        wr.writerow([key])  # ensure proper CSV formatting of the key cell
        df2 = my_dict[key]
        df2.to_csv(f)

With you sample data the content of the csv file is:使用您的样本数据,csv 文件的内容是:

dummy
,Value
M1,0
M2,0
M3,0
"K1::foo(bar::z(x,u))"
,Value,Value
0,10,2
1,5,2
2,10,2
K2::foo()
,Value
0,20
1,10
2,15

You can see the the csv writer has correctly quoted the second key because it contains a comma...您可以看到 csv writer 正确引用了第二个键,因为它包含一个逗号...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM