我正在尝试在django中设置一个网站,该网站允许用户向包含有关其在欧洲议会中的代表的信息的数据库发送查询.我将数据放在逗号分隔的.txt文件中,格式如下:
议会,名称,国家,Party_Group,National_Party,职位
7,Marta Andreasen,英国,欧洲自由民主集团,英国独立党,成员
等等....
我想用这些数据填充一个SQLite3数据库,但到目前为止我发现的所有教程都只显示了如何手动执行此操作.由于我在文件中有736个观察结果,所以我真的不想这样做.
我怀疑这是一件简单的事情,但如果有人能告诉我如何做到这一点,我将非常感激.
托马斯
我有关于2008 - 9年丹麦议会所有选票的数据.问题是每个投票都记录在自己的.txt文件中,格式如下(请参阅R的三个投票的帖子附录):
Name Party Vote
Anders Samuelsen LA For
Rasmus Prehn S Imod
... ... ...
Run Code Online (Sandbox Code Playgroud)
我想使用以下格式将所有文件合并到一个数据框:
Name Party Vote1 Vote2 Vote3
Anders Samuelsen LA For Imod Imod
Rasmus Prehn S Imod For Fraværende
Run Code Online (Sandbox Code Playgroud)
(丹麦语翻译:For = yes,imod = no,FravÃ| rende = absent)
总共我有200多个文件,所以问题是:如何编写一个循环,将所有数据框合并到上面所需的结构中?
附录:
Vote1:
structure(list(V1 = structure(c(2L, 3L, 4L, 7L, 8L, 5L, 6L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, …Run Code Online (Sandbox Code Playgroud) 我有一个向量填充以下格式的字符串: <year1><year2><id1><id2>
向量的第一个条目如下所示:
199719982001
199719982002
199719982003
199719982003
Run Code Online (Sandbox Code Playgroud)
对于第一个条目,我们有:year1 = 1997,year2 = 1998,id1 = 2,id2 = 001.
我想写一个正则表达式,它取出year1,id1和id2的数字不为零.所以对于第一个条目,正则表达式应该输出:199721.
我尝试使用stringr包,并创建了以下正则表达式:
"^\\d{4}|\\d{1}(?<=\\d{3}$)"
Run Code Online (Sandbox Code Playgroud)
拉出year1和id1,然而当使用lookbehind我得到一个"无效的正则表达式"错误.这对我来说有点令人费解,R不能处理前瞻和外观吗?
我已经为macosx Leopard 10.5.8下载了igraph 0.5.4 tar球.当我解压缩然后运行:
sudo python setup.py install
Run Code Online (Sandbox Code Playgroud)
我收到以下很长的错误消息:
Include path: /usr/include /usr/local/include
Library path:
running install
running bdist_egg
running egg_info
writing python_igraph.egg-info/PKG-INFO
writing top-level names to python_igraph.egg-info/top_level.txt
writing dependency_links to python_igraph.egg-info/dependency_links.txt
reading manifest file 'python_igraph.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'python_igraph.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.3-fat/egg
running install_lib
running build_py
creating build
creating build/lib.macosx-10.3-fat-2.6
creating build/lib.macosx-10.3-fat-2.6/igraph
copying igraph/__init__.py -> build/lib.macosx-10.3-fat-2.6/igraph
copying igraph/clustering.py -> build/lib.macosx-10.3-fat-2.6/igraph
copying igraph/colors.py -> build/lib.macosx-10.3-fat-2.6/igraph
copying igraph/configuration.py -> build/lib.macosx-10.3-fat-2.6/igraph
copying igraph/datatypes.py -> build/lib.macosx-10.3-fat-2.6/igraph …Run Code Online (Sandbox Code Playgroud) 我有以下示例代码,我从欧洲议会网站上下载了一份特定立法提案的pdf:
编辑:我最终只是获取链接并将其提供给adobes在线转换工具(请参阅下面的代码):
import mechanize
import urllib2
import re
from BeautifulSoup import *
adobe = "http://www.adobe.com/products/acrobat/access_onlinetools.html"
url = "http://www.europarl.europa.eu/oeil/search_reference_procedure.jsp"
def get_pdf(soup2):
link = soup2.findAll("a", "com_acronym")
new_link = []
amendments = []
for i in link:
if "REPORT" in i["href"]:
new_link.append(i["href"])
if new_link == None:
print "No A number"
else:
for i in new_link:
page = br.open(str(i)).read()
bs = BeautifulSoup(page)
text = bs.findAll("a")
for i in text:
if re.search("PDF", str(i)) != None:
pdf_link = "http://www.europarl.europa.eu/" + i["href"]
pdf = urllib2.urlopen(pdf_link)
name_pdf = …Run Code Online (Sandbox Code Playgroud) 我需要在Tkinter中编写一个gui,它可以选择一个csv文件,读入它并根据csv文件第一行中的名称生成一系列按钮(之后应该使用csv文件中的数据来运行模拟次数).
到目前为止,我已经设法编写了一个将读取csv文件的Tkinter gui,但是我对如何继续下去感到困惑:
from Tkinter import *
import tkFileDialog
import csv
class Application(Frame):
def __init__(self, master = None):
Frame.__init__(self,master)
self.grid()
self.createWidgets()
def createWidgets(self):
top = self.winfo_toplevel()
self.menuBar = Menu(top)
top["menu"] = self.menuBar
self.subMenu = Menu(self.menuBar)
self.menuBar.add_cascade(label = "File", menu = self.subMenu)
self.subMenu.add_command( label = "Read Data",command = self.readCSV)
def readCSV(self):
self.filename = tkFileDialog.askopenfilename()
f = open(self.filename,"rb")
read = csv.reader(f, delimiter = ",")
app = Application()
app.master.title("test")
app.mainloop()
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏!
我将以下数据读入R作为名为"data_old"的数据框:
yes year month
1 15 2004 5
2 9 2005 6
3 15 2006 3
4 12 2004 5
5 14 2005 1
6 15 2006 7
. . ... .
. . ... .
Run Code Online (Sandbox Code Playgroud)
我写了一个小循环来遍历数据并总结每个月/年组合的yes变量:
year_f <- c(2004:2006)
month_f <- c(1:12)
for (i in year_f){
for (j in month_f){
x <- subset(data_old, month == j & year == i, select="yes")
if (nrow(x) > 0){
print(sum(x))
}
else{print("Nothing")}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:我可以在终端中打印每个月/年组合的总和,但是如何将其存储在矢量中?(嵌套循环让我头疼,试图解决这个问题).
托马斯