当前位置: 首页 > 知识库问答 >
问题:

如何删除引号时,我转储到另一个文件?

卓宏达
2023-03-14

我对python非常陌生(2天),所以请容忍我。我正在尝试从readCsv文件发送yaml。py来获取数据。yml文件。我想一切都很好,但我从readCsv发送的导出yaml中得到了报价。py来获取数据。tml。为什么?

这就是奇迹发生的地方,readCsv.py:

import csv
import pandas
import yaml

""" Reading whole csv file with panda library """
df = pandas.read_csv('chord-progressions.csv')

""" Read in csv, but easy read with pandas """
""" print(df) """

""" Convert csv to yml """
text = yaml.dump(
    df.to_dict(orient='records'),
    sort_keys=False, width=72, indent=4)

print(text)
""" Export the recently converted yml to getData.yml """
with open('getData.yml', 'w') as outfile:
    yaml.dump(text, outfile, default_flow_style=False)

在我的getData.yml文件中输出:

"-   1st chord: 4\n    2nd chord: 1\n    3rd chord: 5\n    4th chord: Alternative\n\
  \    Progression: .nan\n-   1st chord: 4\n    2nd chord: 1\n    3rd chord: 5\n \
  \   4th chord: Catchy\n    Progression: .nan\n-   1st chord: 1\n    2nd chord: 1\n\
  \    3rd chord: 1\n    4th chord: Didgeridoo\n    Progression: .nan\n-   1st chord:\
  \ 6\n    2nd chord: 4\n    3rd chord: 5\n    4th chord: Dreadful\n    Progression:\
  \ .nan\n-   1st chord: 6\n    2nd chord: 2\n    3rd chord: 5\n    4th chord: Dreadful\n\
  \    Progression: .nan\n-   1st chord: 6\n    2nd chord: 2\n    3rd chord: 4\n \
  \   4th chord: Endless\n    Progression: .nan\n-   1st chord: 3\n    2nd chord:\
  \ 4\n    3rd chord: 6\n    4th chord: Energetic\n    Progression: .nan\n-   1st\
  \ chord: 5\n    2nd chord: 1\n    3rd chord: 4\n    4th chord: Folk\n    Progression:\
  \ .nan\n-   1st chord: 6\n    2nd chord: 1\n    3rd chord: 4\n    4th chord: Folk\n\
  \    Progression: .nan\n-   1st chord: 5\n    2nd chord: 4\n    3rd chord: 3\n \
  \   4th chord: Flamenco\n    Progression: .nan\n-   1st chord: 5\n    2nd chord:\
  \ 6\n    3rd chord: 5\n    4th chord: Flamenco\n    Progression: .nan\n-   1st chord:\
  \ 4\n    2nd chord: 3\n    3rd chord: 6\n    4th chord: Grunge\n    Progression:\
  \ .nan\n-   1st chord: 5\n    2nd chord: 1\n    3rd chord: 6\n    4th chord: Jazz\n\
  \    Progression: .nan\n-   1st chord: 4\n    2nd chord: 5\n    3rd chord: 4\n \
  \   4th chord: Love\n    Progression: .nan\n-   1st chord: 4\n    2nd chord: 1\n\
  \    3rd chord: 5\n    4th chord: Memories\n    Progression: .nan\n-   1st chord:\
  \ 5\n    2nd chord: 6\n    3rd chord: 4\n    4th chord: Pop\n    Progression: .nan\n\
  -   1st chord: 6\n    2nd chord: 3\n    3rd chord: 7\n    4th chord: Pop\n    Progression:\
  \ .nan\n-   1st chord: 1\n    2nd chord: 4\n    3rd chord: 5\n    4th chord: Rebellious\n\
  \    Progression: .nan\n-   1st chord: 4\n    2nd chord: 5\n    3rd chord: 5\n \
  \   4th chord: Sad\n    Progression: .nan\n-   1st chord: 5\n    2nd chord: 4\n\
  \    3rd chord: 4\n    4th chord: Sad\n    Progression: .nan\n-   1st chord: 4\n\
  \    2nd chord: 5\n    3rd chord: 4\n    4th chord: Sad\n    Progression: .nan\n\
  -   1st chord: 4\n    2nd chord: 1\n    3rd chord: 1\n    4th chord: Sweet\n   \
  \ Progression: .nan\n-   1st chord: 4\n    2nd chord: 1\n    3rd chord: 4\n    4th\
  \ chord: Simple\n    Progression: .nan\n-   1st chord: 5\n    2nd chord: 5\n   \
  \ 3rd chord: 1\n    4th chord: Simple\n    Progression: .nan\n-   1st chord: 4\n\
  \    2nd chord: 1\n    3rd chord: 4\n    4th chord: Wildside\n    Progression: .nan\n\
  -   1st chord: 1\n    2nd chord: 4\n    3rd chord: 6\n    4th chord: Wistful\n \
  \   Progression: .nan\n-   1st chord: 1\n    2nd chord: 5\n    3rd chord: 7\n  \
  \  4th chord: Moody\n    Progression: .nan\n-   1st chord: 1\n    2nd chord: 7\n\
  \    3rd chord: 6\n    4th chord: Moody\n    Progression: .nan\n"


共有2个答案

仉英朗
2023-03-14

text已经是YAML;您不需要对它进行第二次编码。

with open('getData.yml', 'w') as outfile:
    print(text, file=outfile)

with open('getData.yml', 'w') as outfile:
    yaml.dump(
        df.to_dict(orient='records'),
        outfile,
        sort_keys=False,
        width=72,
        indent=4
    )
赏高格
2023-03-14

这里发生的事情是,您两次转储您的yaml。首先,在这方面:

""" Convert csv to yml """
text = yaml.dump(
    df.to_dict(orient='records'),
    sort_keys=False, width=72, indent=4)

此时,text是一个带有yaml值的字符串。

然后,再次将其转储到此处:

""" Export the recently converted yml to getData.yml """
with open('getData.yml', 'w') as outfile:
    yaml.dump(text, outfile, default_flow_style=False)

因为您正在转储的只是一个字符串,所以它将作为字符串显示在您的文件中。您可以将字符串直接写入文件:

with open('getData.yml', 'w') as outfile:
    outfile.write(text)

或者,您可以直接将CSV转储到yaml中,如下所示:

with open('getData.yml', 'w') as outfile:
    yaml.dump(
        df.to_dict(orient='records'), outfile,
        sort_keys=False, width=72, indent=4, default_flow_style=False)
 类似资料:
  • 我对SqlAlchemy和Firebird DB是新手。 我可以直接使用DBeaver创建一个表: 但如果我尝试使用pyndas sqlalchemy执行同样的操作,我会得到一个错误: 问题是由引号

  • 问题内容: 对于此函数,运行后,输出将为2,5,7 .... 9,我的问题是如何删除最后一个逗号? 问题答案: 我会在一开始打印逗号,像这样-

  • 我有一个主类,该类将输出打印到控制台。。。 例如,如果行中包含A和B,我需要打印“Apple” 我将在主方法中通过首先实例化该类的对象来实现这一点,即。 代码示例:

  • 输出为 预期输出

  • 我有一个字符串: "16680,16678,16677,16676,16675,16672" 这是我在调用存储过程传递的参数时得到的。我想把这些记录插在哪里 其中 (16680,16678,16677,16676,16675,16672)。 如何在 Sybase ASE 存储过程中执行此操作?

  • 问题内容: 我正在编写一个Java应用程序以将数据从Oracle导出到CSV文件 不幸的是,数据的内容可能非常棘手。分隔符仍然是逗号,但连续的一些数据可能是这样的: 因此,这是该列上的字符串之一: 我说:“我是5‘10”。 别开玩笑了,我需要在Java生成的CSV文件中的excel或开放式办公室中显示上述注释,而又不能妥协,并且当然不能弄乱其他常规的转义符情况(即,常规的双引号和元组中的常规逗号)