如何从内存中读取TSV文件并正确遍历每一行

时间:2019-07-17 16:38:57

标签: json python-3.x csv

我将把应用程序部署到只读文件系统。该应用程序的一部分涉及处理TSV文件,而不是将其保存在本地,而是使用csv.DictReader作为字典将流文件读入,将其转换为JSON对象,然后将其传递给另一个python脚本,然后从该python脚本中,通过csv.DictWriter将JSON对象保存为TSV文件在内存中。

我现在遇到的问题是正确读取该内存中的TSV文件。当我尝试遍历文件的各行时,它不会以通常的方式打印出来。如何正确遍历内存中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 = ('"[{\\"First_Name\\": \\"Anna\\", \\"Initials\\": \\"A\\", \\"Last_Name\\": '
 '\\"Karenina\\", \\"Places\\": \\"BioInform_Harvard\\", \\"Email\\": '
 '\\"anna.Karenina@ucsf.edu\\", \\"Tel\\": \\"8885006000\\", \\"Fax\\": '
 '\\"8885006001\\", \\"Joint\\": \\"1\\", \\"Corresponding\\": \\"\\", '
 '\\"Experimental design\\": \\"Y\\", \\"Data generation\\": \\"Y\\", \\"Data '
 'processing\\": \\"Y\\", \\"Data analysis\\": \\"Y\\", \\"Statistical '
 'analysis\\": \\"Y\\", \\"Manuscript preparation\\": \\"Y\\"},{\\"First_Name\\": \\"UCSF_Weill\\", \\"Initials\\": \\"Department '
 'of Psychiatry, UCSF Weill Institute for Neurosciences, University of '
 'California, San Francisco, San Francisco, CA 94158\\", \\"Last_Name\\": '
 '\\"\\", \\"Places\\": \\"\\", \\"Email\\": \\"\\", \\"Tel\\": \\"\\", '
 '\\"Fax\\": \\"\\", \\"Joint\\": \\"\\", \\"Corresponding\\": \\"\\", '
 '\\"Experimental design\\": \\"\\", \\"Data generation\\": \\"\\", \\"Data '
 'processing\\": \\"\\", \\"Data analysis\\": \\"\\", \\"Statistical '
 'analysis\\": \\"\\", \\"Manuscript preparation\\": \\"\\"}]"')

# Convert json string that's passed to script into literal list of dicts
contents = literal_eval(json.loads(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()))

# Open the TSV file from memory and process the contents
for line in as_string:
    print(line)

0 个答案:

没有答案