我使用R,我将这个字符串作为一行,我需要将其拆分为列
'id':1050442590754103297,'id_str':'1050442590754103297','name':'امرودينا','screen_name':'uclkGkQ5','location':无,'url':无,'description':'\ u200f \ u200fمنزويالاحتياجاتالخاصه','translator_type':'none','protected':False,'Verified':False,'followers_count':1567,'friends_count':4019,'listed_count':0,'favourites_count':6669 ,'statuses_count':9279,'created_at':'Thu Oct 11 17:46:44 +0000 2018','utc_offset':无,'time_zone':无,'geo_enabled':False,'lang':'ar' ,'contributors_enabled':False,'is_translator':False,'profile_background_color':'F5F8FA','profile_background_image_url':'','profile_background_image_url_https':'','profile_background_tile':False,'profile_link_color':'1DA1F2',' profile_sidebar_border_color':'C0DEED','profile_sidebar_fill_color':'DDEEF6','profile_text_color':'333333','profile_use_background_image':是的,'profile_image_url':'http://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg','profile_image_url_https':'https://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg','profil e_banner_url':'https://pbs.twimg.com/profile_banners/1050442590754103297/1539390015','default_profile':是的,'default_profile_image':错误,'跟随':无,'follow_request_sent':无,'通知':无
我试过这个代码工作但是我需要指定我需要的列数,而且我需要在末尾重命名列,所以很难并且需要时间
d<-str_split_fixed(try$user, ",", 4)
我得到的结果是没有列名:
'id': 1050442590754103297 'id_str': '1050442590754103297' 'name': 'ام رودينا <U+267F>'
第四列包含其余字符串
'screen_name': 'uclkGkQ5', 'location': None, 'url': None, 'description': '\u200f\u200fمن زوي الاحتياجات الخاصه<U+267F>', 'translator_type': 'none', 'protected': False, 'verified': False, 'followers_count': 1567, 'friends_count': 4019, 'listed_count': 0, 'favourites_count': 6669, 'statuses_count': 9279, 'created_at': 'Thu Oct 11 17:46:44 +0000 2018', 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'lang': 'ar', 'contributors_enabled': False, 'is_translator': False, 'profile_background_color': 'F5F8FA', 'profile_background_image_url': '', 'profile_background_image_url_https': '', 'profile_background_tile': False, 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'profile_image_url': 'http://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/1050442590754103297/1539390015', 'default_profile': True, 'default_profile_image': False, 'following': None, 'follow_request_sent': None, 'notifications': None
我需要一个基于逗号分割行的代码,并使列名称为(:)之前的单词,如下所示:
id id_str name screen_name
1050442590754103297 1050442590754103297 ام رودينا \u267f uclkGkQ5
和其他字符串一样希望你理解我,谢谢你
这模仿(但不合法)JSON。一种方法(如果假设为真)将是“转换为JSON”并从那里解析。
前面:我的R会话有一个问题,阿拉伯字母没有正确存储在字符串中。这发生在调用
gsub
之前,所以我相信它可能在你的机器上运行得很好。作为结果,您将在输出中看到空字符串。 (副本在当地搜索这个,我想先给你一些代码。)
假设:
'screen_name':'Is None'
不会发生)NULL
,之前有:None
重要转化:
True
和False
为小写None
到null
{
和}
可以在这里使用magrittr
管道以获得可读性,或者您可以嵌套所有功能(几乎没有更快):
out <- jsonlite::fromJSON(
paste0("{", gsub(":\\s*True\\s*(,?)", ":true\\1",
gsub(":\\s*False\\s*(,?)", ":false\\1",
gsub(":\\s*None\\s*(,?)", ":null\\1",
gsub("'", '"', s)))),
"}"))
# or
library(magrittr)
out <- s %>%
gsub(":\\s*True\\s*(,?)", ":true\\1", .) %>%
gsub(":\\s*False\\s*(,?)", ":false\\1", .) %>%
gsub(":\\s*None\\s*(,?)", ":null\\1", .) %>%
gsub("'", '"', .) %>%
paste0("{", ., "}") %>%
jsonlite::fromJSON(.)
结果(用str
压缩):
str(out)
# List of 39
# $ id : num 1.05e+18
# $ id_str : chr "1050442590754103297"
# $ name : chr " "
# $ screen_name : chr "uclkGkQ5"
# $ location : NULL
# $ url : NULL
# $ description : chr "<U+200F><U+200F> "
# $ translator_type : chr "none"
# $ protected : logi FALSE
# $ verified : logi FALSE
# $ followers_count : int 1567
# $ friends_count : int 4019
# $ listed_count : int 0
# $ favourites_count : int 6669
# $ statuses_count : int 9279
# $ created_at : chr "Thu Oct 11 17:46:44 +0000 2018"
# $ utc_offset : NULL
# $ time_zone : NULL
# $ geo_enabled : logi FALSE
# $ lang : chr "ar"
# $ contributors_enabled : logi FALSE
# $ is_translator : logi FALSE
# $ profile_background_color : chr "F5F8FA"
# $ profile_background_image_url : chr ""
# $ profile_background_image_url_https: chr ""
# $ profile_background_tile : logi FALSE
# $ profile_link_color : chr "1DA1F2"
# $ profile_sidebar_border_color : chr "C0DEED"
# $ profile_sidebar_fill_color : chr "DDEEF6"
# $ profile_text_color : chr "333333"
# $ profile_use_background_image : logi TRUE
# $ profile_image_url : chr "http://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg"
# $ profile_image_url_https : chr "https://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg"
# $ profile_banner_url : chr "https://pbs.twimg.com/profile_banners/1050442590754103297/1539390015"
# $ default_profile : logi TRUE
# $ default_profile_image : logi FALSE
# $ following : NULL
# $ follow_request_sent : NULL
# $ notifications : NULL
笔记:
id
字段转换为numeric
,不能使用bit64
包不能是整数,我不知道这对你来说是否有问题None
后面没有任何东西);尽可能具体,减少在字符串中错误地替换这些文字的可能性