将csv文件拆分为多个txt.档案 [英] Splitting a csv file into multiple txt. files

查看:80
本文介绍了将csv文件拆分为多个txt.档案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的csv数据集,我想将其拆分为多个txt文件.我希望每个文件的名称都来自ID列,每个文件的内容都来自文本列.我的数据看起来像这样.

I have a large csv dataset that I want to split into multiple txt files. I want the name of each file to come from the ID column and the content of each file to come from the Text column. My data looks something like this.

ID      Text
1       I like dogs
2       My name is
3       It is sunny

任何人都可以提供建议吗?我不介意使用excel或R.谢谢!

Would anyone be able to help advise? I don't mind using excel or R. Thank you!

推荐答案

在R中,您可以通过 ID split 数据并使用 writeLines 将其写入文本文件.

In R, You can split the data by ID and use writeLines to write it in text files.

如果您的数据框名为 df ,请尝试:

If your dataframe is called df, try :

temp <- split(df$Text, df$ID)
Map(function(x, y) writeLines(x, paste0(y, '.txt')), temp, names(temp))

这篇关于将csv文件拆分为多个txt.档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆