如何通过csv.DictWriter将csv.DictReader JSON结构写回到TSV文件?

时间:2019-07-15 23:29:57

标签: json python-3.x csv dictionary

我正在创建一个python(Flask)应用程序,该应用程序将在生产环境中部署到只读文件系统。总体而言,该应用程序打算:

  1. 在包装python脚本(csv.DictReader)中将.TSV解析为词典列表(使用main.py
  2. 将该列表转换为JSON结构(仍为main.py
  3. 将此JSON结构作为命令行参数传递给另一个python脚本(author-script.py)(因为我无法在生产环境中本地保存.TSV文件)
  4. 解析JSON结构并写到内存中的临时TSV文件(现在位于author-script.py中)
  5. 处理该TSV文件以生成HTML文件(author-script.py

对于步骤4),我希望author-script.py脚本最终通过csv.DictWriter像.TSV文件一样解析此JSON结构。这是因为最初将author-script.py设置为可解析TSV文件,而修改该文件将花费很长时间,以至于它无法解析字典。

当前,我这样做的方法是从JSON结构回溯到字典,然后通过csv.DictWriter将其写出。以下是我的尝试(仿佛我正在author-script.py中开始解析JSON结构参数一样):

import csv
import json
from pprint import pprint
from ast import literal_eval

# This is the JSON structure that's passed to the python script
infile = '"[{\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Anna\\", \\"Last_Name\\": \\"Karenina\\", \\"Tel\\": \\"8885006000\\", \\"Data generation\\": \\"Y\\", \\"Places\\": \\"BioInform_Harvard\\", \\"Fax\\": \\"8885006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"1\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"anna.Karenina@ucsf.edu\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Konstantin\\", \\"Last_Name\\": \\"Levin\\", \\"Tel\\": \\"8887006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"Neuro_Harvard\\", \\"Fax\\": \\"8887006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"1\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"Konstantin.levin@childrens.harvard.edu\\", \\"Initials\\": \\"D\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Alexei\\", \\"Last_Name\\": \\"Vronsky\\", \\"Tel\\": \\"8889006000\\", \\"Data generation\\": \\"Y\\", \\"Places\\": \\"IGM_Columbia\\", \\"Fax\\": \\"8889006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"2\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"alexei.vronsky@cumc.columbia.edu\\", \\"Initials\\": \\"K\\"}, {\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Stepan\\", \\"Last_Name\\": \\"Oblonsky\\", \\"Tel\\": \\"8891006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"NIMH\\", \\"Fax\\": \\"8891006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"2\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"steoblon@mail.nih.gov\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Ekaterina\\", \\"Last_Name\\": \\"Shcherbatskaya\\", \\"Tel\\": \\"8893006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"CHU_Montreal\\", \\"Fax\\": \\"8893006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"Ekaterina.Shcherbatskaya@umontreal.ca\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Alexei\\", \\"Last_Name\\": \\"Karenin\\", \\"Tel\\": \\"8895006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"BioInform_Harvard, Neuro_Harvard\\", \\"Fax\\": \\"8895006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"Karenin05@hms.harvard.edu\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Fyodor\\", \\"Last_Name\\": \\"Dostoevsky\\", \\"Tel\\": \\"8897006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"Penn_Psych\\", \\"Fax\\": \\"8897006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"Y\\", \\"Email\\": \\"Fyodor_Dostoevsky@pennmedicine.upenn.edu\\", \\"Initials\\": \\"M\\"}, {\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Lev\\", \\"Last_Name\\": \\"Tolstoy\\", \\"Tel\\": \\"8899006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"UCSF_Weill\\", \\"Fax\\": \\"8899006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"Y\\", \\"Email\\": \\"ltolstoy@mednet.ucla.edu\\", \\"Initials\\": \\"N\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Place\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Address\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"BioInform_Harvard\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Biomedical Informatics, Harvard Medical School, Boston, MA, USA\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"CHU_Montreal\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"CHU Sainte-Justine Research Centre, University of Montreal, 3175 Chemin de la Co_te-Sainte-Catherine, Montreal, Quebec H3T 1C5, Canada.\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"IGM_Columbia\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Institute for Genomic Medicine, Columbia University Medical Center, Hammer Health Sciences, 1408, 701 West 168th St., New York, NY 10032, USA.\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Neuro_Harvard\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Neurology, Boston Children\'s Hospital and Harvard Medical School, Boston, MA, USA\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"NIMH\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"National Institute of Mental Health, 10 Center Dr, Bethesda, MD 20814\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Penn_Psych\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Psychiatry, Neuropsychiatry Section, Perelman School of Medicine at the University of Pennsylvania, 3400 Spruce, Philadelphia, PA 19104\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"UCSF_Weill\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Psychiatry, UCSF Weill Institute for Neurosciences, University of California, San Francisco, San Francisco, CA 94158\\"}]"'

# Convert json string that's passed to script into literal list of dicts
contents = literal_eval(json.loads(infile))

# Put list of dicts back into TSV "file" in memory
writer = csv.DictWriter(open("dict_write_test.tsv", 'w'), fieldnames=None, delimiter='\t')
for l in contents:
    writer.writerow(l)

我遇到的错误是TypeError: argument of type 'NoneType' is not iterable。如果我在同一脚本中有csv.DictReader对象,我知道如何使它起作用,但是在这种情况下,我将没有这个对象。

我的问题是

  1. 如何精确地从JSON->字典-> DictReader对象-> DictWriter-> TSV文件转换?
  2. 如何写出内存中的.TSV文件

更新

当我通过在同一代码中使用csv.DictReader对象来测试以下方法时,csv.DictWriter方法显然可以正常工作,并像在原始TSV中一样显示出内存中的TSV文件。但是,当我实现@martineau的解决方案时,列以混乱的方式输出。

infile = open("/Users/cdastmalchi/Downloads/Author_Template.tsv")

contents = csv.DictReader(infile, delimiter='\t')
memory_file = io.StringIO()

writer = csv.DictWriter(memory_file, fieldnames=contents.fieldnames, delimiter='\t')
for row in contents:
    writer.writerow(row)

as_string = memory_file.getvalue()
print('\n'.join(as_string.splitlines()))

1 个答案:

答案 0 :(得分:1)

您收到错误消息,因为您在fieldnames=None通话中有csv.DictWriter()。您可以从contents列表中第一个字典的键中获取字段名称。

可以使用io.StringIO类将TSV文件写入内存。

from ast import literal_eval
import csv
import json
import io
from pprint import pprint

# This is the JSON structure that's passed to the python script
infile = '"[{\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Anna\\", \\"Last_Name\\": \\"Karenina\\", \\"Tel\\": \\"8885006000\\", \\"Data generation\\": \\"Y\\", \\"Places\\": \\"BioInform_Harvard\\", \\"Fax\\": \\"8885006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"1\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"anna.Karenina@ucsf.edu\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Konstantin\\", \\"Last_Name\\": \\"Levin\\", \\"Tel\\": \\"8887006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"Neuro_Harvard\\", \\"Fax\\": \\"8887006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"1\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"Konstantin.levin@childrens.harvard.edu\\", \\"Initials\\": \\"D\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Alexei\\", \\"Last_Name\\": \\"Vronsky\\", \\"Tel\\": \\"8889006000\\", \\"Data generation\\": \\"Y\\", \\"Places\\": \\"IGM_Columbia\\", \\"Fax\\": \\"8889006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"2\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"alexei.vronsky@cumc.columbia.edu\\", \\"Initials\\": \\"K\\"}, {\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Stepan\\", \\"Last_Name\\": \\"Oblonsky\\", \\"Tel\\": \\"8891006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"NIMH\\", \\"Fax\\": \\"8891006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"2\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"steoblon@mail.nih.gov\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Ekaterina\\", \\"Last_Name\\": \\"Shcherbatskaya\\", \\"Tel\\": \\"8893006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"CHU_Montreal\\", \\"Fax\\": \\"8893006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"Ekaterina.Shcherbatskaya@umontreal.ca\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Alexei\\", \\"Last_Name\\": \\"Karenin\\", \\"Tel\\": \\"8895006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"BioInform_Harvard, Neuro_Harvard\\", \\"Fax\\": \\"8895006001\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"Karenin05@hms.harvard.edu\\", \\"Initials\\": \\"A\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Fyodor\\", \\"Last_Name\\": \\"Dostoevsky\\", \\"Tel\\": \\"8897006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"Penn_Psych\\", \\"Fax\\": \\"8897006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"Y\\", \\"Email\\": \\"Fyodor_Dostoevsky@pennmedicine.upenn.edu\\", \\"Initials\\": \\"M\\"}, {\\"Manuscript preparation\\": \\"Y\\", \\"First_Name\\": \\"Lev\\", \\"Last_Name\\": \\"Tolstoy\\", \\"Tel\\": \\"8899006000\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"UCSF_Weill\\", \\"Fax\\": \\"8899006001\\", \\"Experimental design\\": \\"Y\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"Y\\", \\"Statistical analysis\\": \\"Y\\", \\"Data processing\\": \\"Y\\", \\"Corresponding\\": \\"Y\\", \\"Email\\": \\"ltolstoy@mednet.ucla.edu\\", \\"Initials\\": \\"N\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Place\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Address\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"BioInform_Harvard\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Biomedical Informatics, Harvard Medical School, Boston, MA, USA\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"CHU_Montreal\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"CHU Sainte-Justine Research Centre, University of Montreal, 3175 Chemin de la Co_te-Sainte-Catherine, Montreal, Quebec H3T 1C5, Canada.\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"IGM_Columbia\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Institute for Genomic Medicine, Columbia University Medical Center, Hammer Health Sciences, 1408, 701 West 168th St., New York, NY 10032, USA.\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Neuro_Harvard\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Neurology, Boston Children\'s Hospital and Harvard Medical School, Boston, MA, USA\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"NIMH\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"National Institute of Mental Health, 10 Center Dr, Bethesda, MD 20814\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"Penn_Psych\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Psychiatry, Neuropsychiatry Section, Perelman School of Medicine at the University of Pennsylvania, 3400 Spruce, Philadelphia, PA 19104\\"}, {\\"Manuscript preparation\\": \\"\\", \\"First_Name\\": \\"UCSF_Weill\\", \\"Last_Name\\": \\"\\", \\"Tel\\": \\"\\", \\"Data generation\\": \\"\\", \\"Places\\": \\"\\", \\"Fax\\": \\"\\", \\"Experimental design\\": \\"\\", \\"Joint\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical analysis\\": \\"\\", \\"Data processing\\": \\"\\", \\"Corresponding\\": \\"\\", \\"Email\\": \\"\\", \\"Initials\\": \\"Department of Psychiatry, UCSF Weill Institute for Neurosciences, University of California, San Francisco, San Francisco, CA 94158\\"}]"'

# Convert json string that's passed to script into literal list of dicts
contents = json.loads(literal_eval(infile))
fieldnames = contents[0].keys()  # Get TSV fieldnames from first dictionary.

# Put list of dicts back into TSV "file" in memory
memory_file = io.StringIO()
writer = csv.DictWriter(memory_file, fieldnames=fieldnames, delimiter='\t')
writer.writerows(contents)

as_string = memory_file.getvalue()
print('\n'.join(as_string.splitlines()))

如果文件很大,则可能无法将文件作为命令行参数传递。