我读过一些关于 Pandas 的 to_csv( ... etc ...) 的 Python 2 限制。我击中了吗?我在 Python 2.7.3
当 ≥ 和 - 出现在字符串中时,这会变成垃圾字符。除此之外,导出是完美的。
df.to_csv("file.csv", encoding="utf-8")
有什么解决办法吗?
df.head() 是这样的:
demography Adults ≥49 yrs Adults 18−49 yrs at high risk|| \
state
Alabama 32.7 38.6
Alaska 31.2 33.2
Arizona 22.9 38.8
Arkansas 31.2 34.0
California 29.8 38.8
csv 输出是这样的
state, Adults ≥49 yrs, Adults 18−49 yrs at high risk||
0, Alabama, 32.7, 38.6
1, Alaska, 31.2, 33.2
2, Arizona, 22.9, 38.8
3, Arkansas,31.2, 34
4, California,29.8, 38.8
整个代码是这样的:
import pandas
import xlrd
import csv
import json
df = pandas.DataFrame()
dy = pandas.DataFrame()
# first merge all this xls together
workbook = xlrd.open_workbook('csv_merger/vaccoverage.xls')
worksheets = workbook.sheet_names()
for i in range(3,len(worksheets)):
dy = pandas.io.excel.read_excel(workbook, i, engine='xlrd', index=None)
i = i+1
df = df.append(dy)
df.index.name = "index"
df.columns = ['demography', 'area','state', 'month', 'rate', 'moe']
#Then just grab month = 'May'
may_mask = df['month'] == "May"
may_df = (df[may_mask])
#then delete some columns we dont need
may_df = may_df.drop('area', 1)
may_df = may_df.drop('month', 1)
may_df = may_df.drop('moe', 1)
print may_df.dtypes #uh oh, it sees 'rate' as type 'object', not 'float'. Better change that.
may_df = may_df.convert_objects('rate', convert_numeric=True)
print may_df.dtypes #that's better
res = may_df.pivot_table('rate', 'state', 'demography')
print res.head()
#and this is going to spit out an array of Objects, each Object a state containing its demographics
res.reset_index().to_json("thejson.json", orient='records')
#and a .csv for good measure
res.reset_index().to_csv("thecsv.csv", orient='records', encoding="utf-8")
最佳答案
您的“坏”输出是 UTF-8,显示为 CP1252。
在 Windows 上,如果文件开头没有字节顺序标记 (BOM) 字符,许多编辑器会假定默认的 ANSI 编码(美国 Windows 上的 CP1252)而不是 UTF-8。虽然 BOM 对 UTF-8 编码毫无意义,但其 UTF-8 编码的存在可作为某些程序的签名。例如,即使在非 Windows 操作系统上,Microsoft Office 的 Excel 也需要它。试试:
df.to_csv('file.csv',encoding='utf-8-sig')
该编码器将添加 BOM。
关于python - Pandas df.to_csv ("file.csv"encode ="utf-8")仍然为减号提供垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25788037/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que