转储到另一个文件时如何删除引号?
Posted
技术标签:
【中文标题】转储到另一个文件时如何删除引号?【英文标题】:How to remove quotes when I dump to another file? 【发布时间】:2021-10-23 19:56:49 【问题描述】:我对 python 很陌生(2 天),所以请耐心等待。 我正在尝试将 yaml 从文件 readCsv.py 发送到 getData.yml 文件。我认为一切正常,但我收到了从 readCsv.py 发送到 getData.tml 的导出 yaml 的引用。这是为什么呢?
这就是魔法发生的地方,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"
【问题讨论】:
【参考方案1】:这里发生的事情是,您将 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)
【讨论】:
如果您有时间回答,我还有一个问题。这个工作代码运行良好,谢谢,但如果我只想说 csv 文件中的第一行作为 yaml 代码发送和转储。我该如何做到这一点? 只需抓取数据框的第一行。 ***.com/questions/25254016/…【参考方案2】: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
)
【讨论】:
以上是关于转储到另一个文件时如何删除引号?的主要内容,如果未能解决你的问题,请参考以下文章
如何删除 jq 输出中的双引号以在 bash 中解析 json 文件?
在 Java 中调用 Oracle 存储过程时如何避免核心转储