推文JSON转换为Excel

时间:2016-07-03 19:11:51

标签: json r twitter

我试图转换我的JSON文件(这是一个使用流API的推文捕获。有50,000个寄存器),因为这个字符" / n"它是不可能的。 。 (看看例子:http://imgur.com/ymxCrmk) 每当我尝试可视化我的数据时,就会出现换行符(由于/ n),我无法使用我的数据...... 我希望通过清除所有信息来轻松过滤我的数据......

这是我使用的代码

setwd();
file <- "myfile.json"
# as json object
mytweets<-readTweets("myfile")
# as dataframe
tweets.df <- parseTweets("myfile", simplify = FALSE)

任何人都可以帮助我,请????

1 个答案:

答案 0 :(得分:2)

write.csv2(df, tf <- tempfile(fileext = ".csv"), row.names = F)
shell.exec(tf) # open file on windows machine, where .csv is associated with excel

对我来说很好:

enter image description here

帮助?write.csv2

  

write.csv2使用逗号表示小数点,使用分号表示   分隔符,某些西欧的CSV文件的Excel约定   区域设置。

这适用于我的情况。您可能需要对其进行调整,例如:尝试使用\t(制表符)或,(逗号)等其他分隔符,或更改Excel中的默认分隔符。

另一种方法可能是事先替换所有换行符:gsub("\n", "", df$text)

数据...

... dput(head(tweets.df, 3))

df <- structure(list(text = c("RT @louane: Oui @Solidays sera la prochaine date à Paris ! \nmais d'abord @Zenith_Paris ce soir et demain encore !! <ed><U+00A0><U+00BD><ed><U+00B4><U+00A5><ed><U+00A0><U+00BD><ed><U+00B2><U+00A5> https://.../Pf8yNp56…", 
"EgyptAir Jet Disappears Over Mediterranean Sea https://.../SW2yWp6RrG https://.../0xeWSBZlvI", 
"RT @morandiniblog: EN DIRECT - Paris et Le Caire expriment \"leurs condoléances\" aux familles des victimes du crash du vol MS804 https://t.c…"
), retweet_count = c(25, 0, 4), favorited = c(FALSE, FALSE, FALSE
), truncated = c(FALSE, FALSE, FALSE), id_str = c("733225923248566272", 
"733225923621867520", "733225924385247232"), in_reply_to_screen_name = c(NA_character_, 
NA_character_, NA_character_), source = c("<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", 
"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>", 
"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>"
), retweeted = c(FALSE, FALSE, FALSE), created_at = c("Thu May 19 09:20:57 +0000 2016", 
"Thu May 19 09:20:57 +0000 2016", "Thu May 19 09:20:57 +0000 2016"
), in_reply_to_status_id_str = c(NA_character_, NA_character_, 
NA_character_), in_reply_to_user_id_str = c(NA_character_, NA_character_, 
NA_character_), lang = c("fr", "en", "fr"), listed_count = c(33, 
0, 1), verified = c(FALSE, FALSE, FALSE), location = c("Udine ITALY", 
"Instagram: dimple_90s", "Bordeaux "), user_id_str = c("2703557176", 
"2169023166", "865179050"), description = c("NEW VIDEO,NEWS,CHARTS,SPORTS,\nFASHION,ONLY OFFICIAL PAGES..", 
"20. Watford/3rd Year UoW.", "je veux désormais conserver que les moments de bonheur  <U+2728>             communication - Bordeaux"
), geo_enabled = c(TRUE, TRUE, TRUE), user_created_at = c("Sat Jul 12 15:06:22 +0000 2014", 
"Fri Nov 01 20:16:38 +0000 2013", "Sat Oct 06 19:23:29 +0000 2012"
), statuses_count = c(11283, 7481, 20834), followers_count = c(1909, 
305, 140), favourites_count = c(155920, 2413, 2019), protected = c(FALSE, 
FALSE, FALSE), user_url = c(NA_character_, NA_character_, NA_character_
), name = c("MUSIC news!", "George The Mermaid<U+264A><U+FE0F>", 
"Laura"), time_zone = c(NA, NA, "Paris"), user_lang = c("it", 
"en", "fr"), utc_offset = c(NA, NA, 7200), friends_count = c(354, 
278, 329), screen_name = c("barreca51", "GeorgiaWalker95", "laulylove"
), country_code = c(NA_character_, NA_character_, NA_character_
), country = c(NA_character_, NA_character_, NA_character_), 
    place_type = c(NA_character_, NA_character_, NA_character_
    ), full_name = c(NA_character_, NA_character_, NA_character_
    ), place_name = c(NA, NA, NA), place_id = c(NA, NA, NA), 
    place_lat = c(NaN, NaN, NaN), place_lon = c(NaN, NaN, NaN
    ), lat = c(NA_real_, NA_real_, NA_real_), lon = c(NA_real_, 
    NA_real_, NA_real_), expanded_url = c("https://twitter.com/justemoi2013/status/733173507023069184", 
    "http://news.sky.com/story/1698511/egyptair-jet-disappears-over-mediterranean-sea", 
    "http://www.jeanmarcmorandini.com/article-353767-en-direct-disparition-du-vol-paris-le-caire-l-avion-s-est-ecrase-au-large-de-l-ile-grecque-de-karpathos-dans-l-espace-aerien-egyptien.html"
    ), url = c("https://.../Pf8yNp56Th", "https://.../SW2yWp6RrG", 
    "https://.../hok65e82pV")), .Names = c("text", "retweet_count", 
"favorited", "truncated", "id_str", "in_reply_to_screen_name", 
"source", "retweeted", "created_at", "in_reply_to_status_id_str", 
"in_reply_to_user_id_str", "lang", "listed_count", "verified", 
"location", "user_id_str", "description", "geo_enabled", "user_created_at", 
"statuses_count", "followers_count", "favourites_count", "protected", 
"user_url", "name", "time_zone", "user_lang", "utc_offset", "friends_count", 
"screen_name", "country_code", "country", "place_type", "full_name", 
"place_name", "place_id", "place_lat", "place_lon", "lat", "lon", 
"expanded_url", "url"), row.names = c(NA, 3L), class = "data.frame")