我尝试将 Anova 结果写入 txt。但是,我发现 write 函数无法处理任务。
我的代码如下:
##first I calculate the Anova of two variable and then stored the result in "a".
functionanova<-function(x,y,datasource){
result= aov(y ~ x, data= datasource)
return(summary(result))
}
a<-functionanova(df1$Pe,df1$Pb,df1)
##The result is:
a
Df Sum Sq Mean Sq F value Pr(>F)
x 1 2.77 2.773 0.662 0.419
Residuals 68 284.85 4.189
> class(a)
[1] "summary.aov" "listof"
##Then I try to write it to an text. However,there is an error with this type of data.
> write(a, "d:\\week1\\MYOUTFILE.txt", …Run Code Online (Sandbox Code Playgroud) 我使用urlopen获取如下数据字符串.我想将字符串转换为数据帧并保留几个列,如state,AQI等.我不知道该怎么做,想向你寻求建议.谢谢!
response=urlopen(URL).read().decode('utf-8')
print(response)
"DateIssue","DateForecast","ReportingArea","StateCode","Latitude","Longitude","ParameterName","AQI","CategoryNumber","CategoryName","ActionDay","Discussion"
"2017-05-01 ","2017-05-01 ","Metropolitan Washington","DC","38.919","-77.013","O3","42","1","Good","false",""
"2017-05-01 ","2017-05-01 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","46","1","Good","false",""
"2017-05-01 ","2017-05-02 ","Metropolitan Washington","DC","38.919","-77.013","O3","44","1","Good","false",""
"2017-05-01 ","2017-05-02 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","25","1","Good","false",""
"2017-05-01 ","2017-05-03 ","Metropolitan Washington","DC","38.919","-77.013","O3","44","1","Good","false",""
"2017-05-01 ","2017-05-03 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","25","1","Good","false",""
"2017-05-01 ","2017-05-04 ","Metropolitan Washington","DC","38.919","-77.013","O3","42","1","Good","false",""
"2017-05-01 ","2017-05-04 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","29","1","Good","false",""
Run Code Online (Sandbox Code Playgroud) 我现在尝试输出用塞尔维亚语编写的数据框。首先,我尝试使用 utf-8 或 utf-8-sig 编解码器。有多个列,这是正确的。但是输出的csv文件存在乱码问题。然后我尝试使用 utf-16 输出数据帧。这次编解码器是正确的。但输出的 csv 只有一列,例如:
我的代码是:
df1.to_csv('people.csv', encoding = "utf-16",index = False)
Run Code Online (Sandbox Code Playgroud)
原始数据帧的头部是:
如果我使用 utf-16,输出为(编解码器正确但列不正确):
我该如何解决这个问题。谢谢!