【问题标题】:Convert pandas DataFrame into string to be written to a cfg file将 pandas DataFrame 转换为要写入 cfg 文件的字符串
【发布时间】:2018-07-16 20:34:59
【问题描述】:

目标

我有一个 Pandas 数据框,如下所示,我想加入 commandvalue 列,同时将其转换回原始字符串格式以写入 .cfg 文件。


数据框 - df:

之前加入:

   command                                              value
0     bind                                       "0" "slot10"
1     bind                                        "1" "slot1"
2     bind                                        "2" "slot2"
3     bind                                        "3" "slot3"
4     bind                                        "4" "slot4"
5     bind                                        "5" "slot5"
6     bind                                        "6" "slot6"
7     bind                                        "7" "slot7"
8     bind                                        "8" "slot8"
9     bind                                        "9" "slot9"
10    bind                                    "a" "+moveleft"
11    bind                                      "b" "buymenu"
12    bind                                   "d" "+moveright"

之后加入:

0                          bind "0" "slot10"
1                           bind "1" "slot1"
2                           bind "2" "slot2"
3                           bind "3" "slot3"
4                           bind "4" "slot4"
5                           bind "5" "slot5"
6                           bind "6" "slot6"
7                           bind "7" "slot7"
8                           bind "8" "slot8"
9                           bind "9" "slot9"
10                      bind "a" "+moveleft"
11                        bind "b" "buymenu"
12                     bind "d" "+moveright"
13                           bind "e" "+use"
14                  bind "f" "+lookatweapon"
...
etc.
dtype: object

我的尝试

我已经设法使用以下代码将两列组合起来以获得上面的输出:

df = df['command'].astype(str)+' '+df['value'].astype(str)

但是,这仍然没有帮助将数据帧转换为原始字符串,以便可以将其写入 .cfg 文件。

我也尝试过使用df.to_string(),但这似乎没有什么不同


预期输出

我想获得像这样分配在要写入 .cfg 文件的变量下的原始字符串输出:

bind "0" "slot10"
bind "1" "slot1"
bind "2" "slot2"
bind "3" "slot3"
bind "4" "slot4"
bind "5" "slot5"
bind "6" "slot6"
bind "7" "slot7"
bind "8" "slot8"
bind "9" "slot9"
bind "a" "+moveleft"
bind "b" "buymenu"
bind "d" "+moveright"
bind "e" "+use"
bind "f" "+lookatweapon"
...
etc.

【问题讨论】:

    标签: python python-2.7 pandas dataframe


    【解决方案1】:

    打电话后

    df = df['command'].astype(str)+' '+df['value'].astype(str)
    

    您实际上留下了一个Series 对象,因此您可以调用df.tolist(),然后用换行符连接列表的元素。像这样的

    s = df['command'].astype(str)+' '+df['value'].astype(str)
    cfg_output = '\n'.join(s.tolist()) # joins each command with a newline
    

    由于您在原始配置文件中的读取方式,您在最后一行得到了一个 None 值,这一行

    df = pd.DataFrame(df[0].str.split(' ',1).tolist(),columns = ['command','value'])
    

    无法计算没有值的配置设置。考虑原始 conf 文件的最后三行

    sensitivity "0.9"
    cl_teamid_overhead_always 1
    host_writeconfig
    

    sensitivitycl_teamid_overhead_always 的设置后面有值,但 host_writeconfig 没有,所以当 pandas 尝试在空格(该行中不存在)上拆分时,第一个值是 @ 987654332@,第二个是 None 对象。

    【讨论】:

    • 非常感谢!那太棒了。输出的底部似乎有 3 个随机的 None 语句。这应该/是否有所作为?
    • 如果没有看到您的输入,我很难说出来
    • 那不是你的“输入”,那是你正在执行的代码。这里的输入将是"E:\\Steam\\userdata\\.........................config.cfg" 的内容
    • 哦,好的,谢谢。这是contents。谢谢你。 (该程序只允许用户更改配置中的值)
    • 我已经弄清楚为什么会出现两个 None 语句。似乎配置文件末尾有两个空行导致错误。但是,它仍然只出现在最后一条命令之后:hastebin.com/ofurobujeb.bash
    猜你喜欢
    • 1970-01-01
    • 2020-04-12
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2018-07-13
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多