我正在data.table使用示例进行学习,并且坚持自己的方案。
我正在使用cars数据集并转换data.table为尝试我的命令。
library(data.table)
> cars.dt=data.table(cars)
> cars.dt[1:5]
speed dist
1: 4 2
2: 4 10
3: 7 4
4: 7 22
5: 8 16
.
.
Run Code Online (Sandbox Code Playgroud)
我想为每个组计算摘要统计信息speed,并将其存储在不同的列中,但值存储在多行中。
例如
> cars.dt[, summary(dist), by="speed"]
speed V1
1: 4 2
2: 4 4
3: 4 6
4: 4 6
5: 4 8
---
110: 25 85
111: 25 85
112: 25 85
113: 25 85
114: 25 85
Run Code Online (Sandbox Code Playgroud)
我期待以下输出,但我无法实现。
speed Min. 1st Qu. Median Mean …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习字符串模板模块并面临使用替代分隔符的问题。
temp_text_dollar变量名带有前缀$并且工作正常
>>> import string
>>> val = {'a1':'VAL1' , 'a2' : 'VAL2' , 'a3' : 'VAL3' , 'a4' : 'VAL4' }
>>> temp_text_dollar = string.Template(" This is a sample text ${a1} $a3 ")
>>> print temp_text_dollar.substitute(val)
This is a sample text VAL1 VAL3
>>> print temp_text_dollar.delimiter
$
>>> print temp_text_dollar.idpattern
[_a-z][_a-z0-9]*
>>> print temp_text_dollar.template
This is a sample text ${a1} $a3
Run Code Online (Sandbox Code Playgroud)
temp_text_pct 变量名称带有前缀,%但它不起作用。
>>> temp_text_pct = string.Template(" This is a sample text %a1 %a3 …Run Code Online (Sandbox Code Playgroud) 我试图使用urllib2和ElementTree解析python中的HTML页面,我在解析HTML时遇到了麻烦.网页在引用的字符串中包含"&"但ElementTree会为包含&的行抛出parseError
脚本:
import urllib2
url = 'http://eciresults.nic.in/ConstituencywiseU011.htm'
req = urllib2.Request(url, headers={'Content-type': 'text/xml'})
r = urllib2.urlopen(req).read()
import xml.etree.ElementTree as ET
htmlpage=ET.fromstring(r)
Run Code Online (Sandbox Code Playgroud)
这会在Python 2.7中引发跟随错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1282, in XML
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1624, in feed
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1488, in _raiseerror
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 676, column 73
Run Code Online (Sandbox Code Playgroud)
错误对应于以下行
<input type="hidden" id="HdnFldAndamanNicobar" value="1,Andaman & Nicobar Islands;" />
Run Code Online (Sandbox Code Playgroud)
看起来当读取HTML页面时,&符号不会像&变量r 那样被解析
我试图使用R程序使用htmlTreeParse解析,"&"转换为&正确.
如果我在urllib2中遗漏了任何内容,请告诉我
编辑:我将"&"替换为"&" &但是第904行包含<javascript中的符号,这会引发相同的错误.应该有一个更好的选择,而不是替换字符. …