Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

sqlite - Export CSV without col.names

I need to take a data.frame and export it to a CSV file (or something else, but CSV seemed like the easiest well-formed format) so I can import it into an SQLite database.

However, it looks like write.csv() requires that I write a header line, and SQLite's .import command requires that I don't have a header line. So that's a bit of a mismatch.

Here's what happens if I try to omit the header line:

> write.csv(mydf, "/tmp/mydf.csv", row.names=F, col.names=F)
Warning message:
In write.csv(mydf, "/tmp/mydf.csv", row.names = F, col.names = F) :
  attempt to set 'col.names' ignored

I have to wonder why it's enforcing that in the first place - the manual says "These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file. Attempts to change append, col.names, sep, dec or qmethod are ignored, with a warning." But I know of nothing in the spec or elsewhere requiring column names - indeed, most tools (Excel, etc.) don't treat them specially.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you can't beat 'em, join 'em.

If you switch to write.table() (which write.csv() calls anyway) you're golden:

R> write.table(trees, file="/tmp/trees.csv", 
+              row.names=FALSE, col.names=FALSE, sep=",")
R> system("head /tmp/trees.csv")
8.3,70,10.3
8.6,65,10.3
8.8,63,10.2
10.5,72,16.4
10.7,81,18.8
10.8,83,19.7
11,66,15.6
11,75,18.2
11.1,80,22.6
11.2,75,19.9
R>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...