我正在玩学习python并尝试将github问题变成可读的形式.使用有关如何将JSON转换为CSV的建议?我想出了这个:
import json
import csv
f=open('issues.json')
data = json.load(f)
f.close()
f=open("issues.csv","wb+")
csv_file=csv.writer(f)
csv_file.writerow(["gravatar_id","position","number","votes","created_at","comments","body","title","updated_at","html_url","user","labels","state"])
for item in data:
csv_file.writerow([item["gravatar_id"], item["position"], item["number"], item["votes"], item["created_at"], item["comments"], item["body"], item["title"], item["updated_at"], item["html_url"], item["user"], item["labels"], item["state"]])
Run Code Online (Sandbox Code Playgroud)
其中"issues.json"是包含我的github问题的json文件.当我尝试运行时,我明白了
File "foo.py", line 14, in <module>
csv_file.writerow([item["gravatar_id"], item["position"], item["number"], item["votes"], item["created_at"], item["comments"], item["body"], item["title"], item["updated_at"], item["html_url"], item["user"], item["labels"], item["state"]])
TypeError: string indices must be integers
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?哪个是"字符串索引"?我敢肯定,一旦我开始工作,我会有更多的问题,但就目前而言,我只是喜欢这个工作!
更新:
当我for简单地调整声明时
for item in data:
print item
Run Code Online (Sandbox Code Playgroud)
我得到的是......"问题" - 所以我做了一些更基本的错误.这是我的一些json:
{"issues":[{"gravatar_id":"44230311a3dcd684b6c5f81bf2ec9f60","position":2.0,"number":263,"votes":0,"created_at":"2010/09/17 16:06:50 -0700","comments":11,"body":"Add missing paging (Older>>) links...
Run Code Online (Sandbox Code Playgroud)
当我打印 …
我仍然习惯于使用python约定并使用它pylint来使我的代码更加pythonic,但我对pylint不喜欢单个字符变量名这一事实感到困惑.我有几个像这样的循环:
for x in x_values:
my_list.append(x)
Run Code Online (Sandbox Code Playgroud)
当我跑pylint,我得到Invalid name "x" for type variable (should match [a-z_][a-z0-9_]{2,30}- 这表明有效的变量名称必须在3到31个字符之间,但我已经查看了PEP8命名约定,我没有看到关于单个小写字母的任何明确的,我确实看到很多使用它们的例子.
PEP8中是否存在我缺少的东西,或者这是pylint独有的标准?
如果在iPython或任何多行命令中引入for循环,我该如何返回并向其添加行?我跑了这个:
for row in table.find_all('tr'):
cells = row.find_all('td')
for c,cell in enumerate(cells):
print c,":",cell.get_text().strip()
try:
this = cells[0]
that = cells[1]
the_docket = cells[2]
other_thign = cells[3]
jumble = re.sub('\s+',' ',str(cells[5])).strip()
except:
"Nope"
Run Code Online (Sandbox Code Playgroud)
并意识到我需要为它添加一行,但我不能只是在运行命令的iPython,b/c中点击"输入".那么我可以在iPython中编辑那个多行命令吗?
我正在使用默认字典.我需要pprint.
然而,当我pprint......这就是它的样子.
defaultdict(<functools.partial object at 0x1f68418>, {u'300:250': defaultdict(<functools.partial object at 0x1f683c0>, {0: defaultdict(<type 'list'>, {u'agid1430864021': {u'status': u'0', u'exclude_regi..........
Run Code Online (Sandbox Code Playgroud)
如何pprint使用默认字典?
我安装了虚拟环境sudo pip install virtualenv但是当我运行时python -m venv flask我仍然得到这个:/usr/bin/python: No module named venv
版本,如果相关:
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
Python 2.7.9
Run Code Online (Sandbox Code Playgroud)
我在这里想念的是什么?
我做了sudo pip install BeautifulSoup4并得到了非常乐观的回应:
Downloading/unpacking beautifulsoup4
Running setup.py egg_info for package beautifulsoup4
Installing collected packages: beautifulsoup4
Running setup.py install for beautifulsoup4
Successfully installed beautifulsoup4
Cleaning up..
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用import BeautifulSoup4或from BeautifulSoup4 import BeautifulSoup4在脚本中时,python说这个名称没有模块.
> import BeautifulSoup
ImportError: No module named BeautifulSoup
Run Code Online (Sandbox Code Playgroud)
更新:pip告诉我,beautifulsoup4 in /usr/local/lib/python2.6/dist-packages但我正在运行2.7.2+(并print sys.path看到2.7路径)...所以现在我需要弄清楚为什么pip把东西放在错误的地方.
我有一个highcharts表,其中包含两个使用命名值的数据系列.在我的一个系列的工具提示中,我想引用该系列中的数据点.所以这个答案中的解决方案:如何在同一图形的每条曲线中的Highcharts上使用不同的格式化程序?对我没有帮助.我需要的不仅仅是tooltipText,我需要一个格式化程序:
一个:
formatter: function() {
return this.x + ': ' + this.series.name +
'<br> $' + Highcharts.numberFormat(this.y, 0);
}
Run Code Online (Sandbox Code Playgroud)
对于另一个:
formatter: function() {
return 'In ' + this.x + ' the median value was' + this.median +
'and the total $' + Highcharts.numberFormat(this.y, 0);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用reveal.js并试图理解如何将我的幻灯片强制到页面的左上角.这似乎应该是直截了当的,但是当我使用Element检查器时,它会彻底改变我甚至无法开始将幻灯片移动到顶部的页面.
将此添加到我的主题:
.reveal .slides>section,
.reveal .slides>section>section {
padding: 0;}
Run Code Online (Sandbox Code Playgroud)
把它搞得一团糟(reveal.css的填充设置为20px 0),但每个幻灯片的顶部仍有空白区域.
我是aws cli的新手,我在文档中花了相当多的时间,但我无法弄清楚如何在上传文件后设置文件权限.所以,如果我上传了一个文件:
aws s3 cp assets/js/d3-4.3.0.js s3://example.example.com/assets/js/
并没有设置访问权限,我需要一种方法来设置它们.chmod 644在aws cli中是否有相同的功能?
那么有没有办法查看访问权限?
我知道我可以使用--acl public-read标志,aws s3 cp但如果我没有,我可以设置访问而不重复完整的复制命令吗?
我正在抓一个包含日期信息的页面.所以我有一个名为warrant_issuedcontains 的变量u'11/5/2003'- 我希望将其存储为机器可读日期.PHP有一个非常方便的strtotime功能.我希望datetime的strptime可以帮助我,但它似乎在我的版本中没有datetime- 这里是我的标签中的所有内容完成datetime.
In [231]: datetime.
datetime.MAXYEAR datetime.__hash__ datetime.__sizeof__
datetime.MINYEAR datetime.__init__ datetime.__str__
datetime.__class__ datetime.__name__ datetime.__subclasshook__
datetime.__delattr__ datetime.__new__ datetime.date
datetime.__dict__ datetime.__package__ datetime.datetime
datetime.__doc__ datetime.__reduce__ datetime.datetime_CAPI
datetime.__file__ datetime.__reduce_ex__ datetime.time
datetime.__format__ datetime.__repr__ datetime.timedelta
datetime.__getattribute__ datetime.__setattr__ datetime.tzinfo
Run Code Online (Sandbox Code Playgroud)
我正在使用iPython 2.7.2+
我在这里吠叫错了吗?u'11/5/2003'变成约会的最佳方式是什么?
python ×7
pip ×2
amazon-s3 ×1
css ×1
datetime ×1
github ×1
highcharts ×1
ipython ×1
javascript ×1
json ×1
pylint ×1
python-venv ×1
reveal.js ×1