从文件中读取Python中元组列表的列表

时间:2016-10-16 20:08:16

标签: python serialization pickle

我想从文件中读取和写入元组列表列表。

library(rvest)
library(tm)
url<-paste0("http://www.tjrj.jus.br/search?q=acidente+de+transito+crianca+atropelamento&btnG=Pesquisar&processType=cnj&site=juris&client=juris&output=xml_no_dtd&proxystylesheet=juris&entqrm=0&oe=UTF-8&ie=UTF-8&ud=1&filter=0&getfields=*&partialfields=(ctd:1)&exclude_apps=1&ulang=en&lr=lang_pt&sort=date:D:S:d1&as_q=+&access=p&entqr=3&start=",seq(0,462,10))

css<-sprintf(".margin-top-10:nth-child(%.d) .outros .featured",1:10)

for (j in 1:1){ # There 47 pages, but I only put one here 
for (i in 1:10){ # there are 10 files per page.
a<-html_node(css=css[i]) %>% 
html_attr("href")
download.file(a,paste0("doc",j,i,".pdf"))
}
}

files <- list.files(pattern = "pdf$")
Rpdf <- readPDF(control = list(text = "-layout"))
docs <- Corpus(URISource(files,encoding="UTF-8"),readerControl = list(reader = Rpdf,language="portuguese"))

我用过

  • g_faces = [[(3,2)(3,5)],[(2,4)(1,3)(1,3)],[(1,2),(3,4),(6,7)]]
  • pickle.dump(g_faces, fp)

但该文件不是人类可读的。有没有简单的方法呢?

1 个答案:

答案 0 :(得分:0)

试试json模块。

import json

g_faces = [[(3,2), (3,5)],[(2,4), (1,3), (1,3)],[(1,2), (3,4), (6,7)]]

json.dump(g_faces, open('test.json', 'w'))

g_faces = json.load(open('test.json'))

# cast back to tuples
g_faces = [[tuple(l) for l in L] for L in g_faces]