简体   繁体   English

如何将所有变量从项目导出到.csv,以便随后将其导入Python环境?

[英]How do I export all variables from a project to .csv so that I can then import them into a Python environment?

I have a large project in RStudio that I would like to attempt some things with in Python and want to know if there is a way of effectively packaging up all of the environment variables (data frames, lists, atomic vectors, etc.) and importing those into Spyder (Python). 我在RStudio中有一个大型项目,我想在Python中尝试一些操作,并且想知道是否存在一种有效包装所有环境变量(数据框,列表,原子向量等)并导入的方法。将它们导入Spyder(Python)。

If not directly possible, I am aware that I can read files into Python in numerous ways, and as such am wondering if there is a method I could use to iterate through my R environment and save all of the variables as .csv files so that I can later read them into Spyder? 如果不是直接可行的话,我知道我可以通过多种方式将文件读入Python,因此我想知道是否存在一种可用于迭代R环境并将所有变量另存为.csv文件的方法,以便我以后可以将它们读入Spyder吗?

I have tried this code but to no avail: 我已经尝试过此代码,但无济于事:

files <- mget(ls())

for (i in 1:length(files)){
  write.csv(files[[i]], paste(names(files[i]), ".csv", sep = ""))
}

I would recommend looking at some of the packages for transferring data, but to specifically answer your question: 我建议您查看一些用于传输数据的软件包,但要专门回答您的问题:

x1 <- "hello"
x2 <- "these are some"
x3 <- "objects"

for (i in names(.GlobalEnv)) {
  write.csv(.GlobalEnv[[i]], file = sprintf("%s.csv", i))
}

You could more explicitly export the objects by storing the objects for export in a separate environment, too. 您也可以通过将要存储的对象存储在单独的环境中来更明确地导出对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何将字典值解析为2个单独的值,以便我可以导出为.csv? - Python: How do I get the dictionary value to be parsed as 2 separate values so I can export as a .csv? Python:如何导入所有变量? - Python: How can I import all variables? 你如何创建一个 GitHub Repository 以便我可以自动导入和导出 python 脚本? - How do you create a GitHub Repository so that I can automatically import and export python scripts? 如何从文本文件中获取每一行并将其拆分,以便我可以在 python 中单独使用它们 - How do I get each line from a text file and split it so that I can use them all separately in python 如何让 python 将我抓取的变量导出为 a.csv? - How can I have python export my scraped variables as a .csv? 如何使用 python 将变量从 .mat 文件(由 Dymola 生成)导出到 .csv - How can I export variables from .mat file (generated by Dymola) to .csv using python Python:如何从 OpenStreetMaps 导入水道作为图形,以便我可以使用 networkx 进行图形分析 - Python: How can I import waterways from OpenStreetMaps as a graph so I can do graph analysis with networkx 如何从CSV文件中提取数据列并将其定义为x和y变量,然后使用pylab在python中绘制它们? - How can I extract columns of data from a CSV file and define them as x and y variables, then plot them in python using pylab? 如何将所有全局变量导入函数-Python - How do I import all global variables into a function - Python 如果我有一个CSV文件的Python列表,如何将它们全部合并为一个巨型CSV文件? - If I have a Python list of CSV files, how do I merge them all into one giant CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM