In [2]:
import xml.etree.ElementTree as ET
import pandas as pd

In [3]:
# tree = ET.ElementTree(file='/home/jeffmxh/NLPCC2014微博情绪分析样例数据.xml')
# tree_2013 = ET.ElementTree(file = '/home/jeffmxh/微博情绪样例数据V5-13.xml')
new_tree = ET.ElementTree(file='/home/da/Downloads/SogouCA/news.allsites.850805.utf8.converted')


  File "<string>", line unknown
ParseError: junk after document element: line 7, column 0

In [22]:
# root = tree.getroot()
# root.tag, root.attrib


Out[22]:
('SampleData', {})

In [23]:
# print(root[0].attrib)
# print(root[0].text)


{'emotion-type2': 'none', 'id': '1', 'emotion-type1': 'disgust'}

    

In [24]:
# print('root[0]:',root[0].attrib, '\n')
# for child_of_root in root[0]:
#     print(child_of_root.text)
#     print(child_of_root.attrib)


root[0]: {'emotion-type2': 'none', 'id': '1', 'emotion-type1': 'disgust'} 

今儿老爸逮着我一顿狠念!
{'keyexpression1': 'null', 'id': '1', 'opinionated': 'N'}
念得我各种烦躁……
{'keyexpression1': '各种烦躁', 'emotion-2-type': 'none', 'id': '2', 'emotion-1-type': 'disgust', 'opinionated': 'Y'}
我要不要考虑下降低回家的频率啊 回来一次吵一次= =真烦人!!!
{'keyexpression1': '真烦人', 'emotion-2-type': 'none', 'id': '3', 'emotion-1-type': 'disgust', 'opinionated': 'Y'}

In [3]:
sentence_list = list()
emotion1 = list()
emotion2 = list()
# keysent = list()
for elem in tree.iter(tag='sentence'):
    if elem.attrib['opinionated']=='Y':
        sentence_list.append(elem.text)
        emotion1.append(elem.attrib['emotion-1-type'])
        emotion2.append(elem.attrib['emotion-2-type'])
    else:
        sentence_list.append(elem.text)
        emotion1.append('none')
        emotion2.append('none')
for elem in tree_2013.iter(tag='sentence'):
    if elem.attrib['emotion_tag']=='Y':
        sentence_list.append(elem.text)
        emotion1.append(elem.attrib['emotion-1-type'])
        emotion2.append(elem.attrib['emotion-2-type'])
    else:
        sentence_list.append(elem.text)
        emotion1.append('none')
        emotion2.append('none')
#         keysent.append(elem.attrib['keyexpression1'])
    #print(elem.tag, elem.text, elem.attrib)

In [4]:
len(sentence_list)
# sentence_frame = pd.DataFrame('sentence':sentence_list,
#                               'keysent':keysent,
#                               'emotion1':emotion1,
#                               'emotion2':emotion2)
sentence_frame = pd.DataFrame()
sentence_frame['sentence'] = sentence_list
# sentence_frame['keysent'] = keysent
sentence_frame['emotion_1'] = emotion1
sentence_frame['emotion2'] = emotion2
sentence_frame


Out[4]:
sentence emotion_1 emotion2
0 今儿老爸逮着我一顿狠念! none none
1 念得我各种烦躁…… disgust none
2 我要不要考虑下降低回家的频率啊 回来一次吵一次= =真烦人!!! disgust none
3 在考虑是否要回刷2.3Rom,黑/锁屏状态,屏幕微微亮; none none
4 发烫耗电快… none none
5 这让我蛋疼! disgust none
6 不过,喜欢4.0原生态的界面! like none
7 去年的今天我去上了政治辅导班,回来看芒果台的跨年晚会,然后叠了纸飞机在上面许下了心愿。 none none
8 又要重复我每年必说的台词了,2010就要过去了,我很怀念它。 like none
9 至少,它看上去很美。 like none
10 欢迎你,2011,新年快乐! happiness like
11 下午到台里,吴凤花和方亚芬正在换装。 none none
12 我刚过去打个招呼,就被德才老师说耽误他们录像。 none none
13 我那个冤呢。 sadness none
14 我说我没采访。 none none
15 他说聊天也不行,耽误穿服装。 none none
16 方姐姐说腿好的话就来段对花枪了。 none none
17 好难过 为什么每天都回忆你 为什么每天都想起你 为什么每天都好痴累 早结束了 怎么还在爱 sadness none
18 为什么我的房间那么干呢,嗓子疼啊~! sadness none
19 今早起来更体现出了我昨天排练的成果,腰酸背痛!!! sadness none
20 不过彭老师今早出门说的那句话我也赞同...... none none
21 这家位于槟城George Town,Tunes Hotel斜对面的饼家,家庭式作业,她的咸蛋... like none
22 今天是个值得开心和崩溃的日子~ sadness happiness
23 大腿再次拉伤,痛到我眼泪都出了… sadness none
24 回到宿舍连续拉肚子,崩溃… sadness none
25 接下来继续红红的红歌赛~ happiness none
26 OMG,是我不关心八卦太久了吗?! surprise none
27 白天还在跟小黑讨论Jensen过生日,跟大J一起回温哥华机场的事,我忍不住想JQ肯定又燃烧起... none none
28 结果刚刚KANA来跟我说八卦:J2周末一起公然出现在某个GAY BAR(CLUB 560),... surprise none
29 谁能告诉我这是不是真的啊啊啊啊啊啊,我要疯狂了~~~~~~~~~ surprise none
... ... ... ...
16676 3学会宽容。宽容像春天的阳光,照耀别人也温暖自己。 none none
16677 4学会隐忍。隐而不发不是不发。 none none
16678 5学会简单。美丽和幸福都是简单的,把握了简单就是不简单。 none none
16679 6学会换位思考。换个角度,世界就会大不相同.. none none
16680 thx @王力群Alex none none
16681 东南网-海峡都市报讯 昨日下午2时许,送完孩子准备回家的万女士,路过福州五四北路省体东门前路... none none
16682 事故发生在往二环路的车道上,肇事小车停在斑马线上,伤者已被送往医院。 none none
16683 目击者林女士说,当时万女士就站在斑马线上准备过马路,可人没动,这车就冲过来了。 none none
16684 太古三里屯北区-围合式分布的旗舰街区,商铺以纵向分割为主(1-2/3-4),沿袭南区1-2层... none none
16685 品牌:阿一鲍鱼、阿玛尼、巴黎世家、浪凡、范思哲、思睿、万宝龙、劳力士、APE、派、我的2次方 none none
16686 我................ none none
16687 轻度失忆症,轻度妄想症,轻度自闭症,轻度抑郁症。 none none
16688 一个月配三次锁匙,我都唔知我搞乜! none none
16689 我仲以为前几晚醉左翻黎系我自己开门,原来系打电话叫人开门噶! none none
16690 唉,千杯不醉变左三杯就醉! none none
16691 连自己醉左都唔知,翻时好清醒甘,训醒就完全断片![左哼哼] none none
16692 近日,甘肃省政府向14个市(州)、省政府有关部门发出《通知》,印发经国务院批准的《甘肃省土地... none none
16693 金厉旭我只在乎你真的好好听哦崇拜! like none
16694 下午场都录嘞,现在休息休惨了哈哈哈哈哈,崔始源今天被说崔老板,被叫到后来他说我不是他们老板,... like happiness
16695 玩游戏,看电影,小姑娘吃过西瓜摸着肚子睡着了…… none none
16696 一看就舒服,这才是完美的夏天~我想放暑假! like happiness
16697 还有谁想放暑假? none none
16698 腿上的伤口好明显 T T 这又不是玩命啊 去他的节目啊咱本来就够优秀了不是吗? anger none
16699 戴了护膝还摔成这样那冲击是有多大[泪] surprise none
16700 麦兜说:有事情是要说出来的,不要等着对方去领悟,因为对方不是你,不知道你想要什么,等到最后只... none none
16701 一个做二手车生意的哥哥和我说时我还不信,这下看来是真的了。 none none
16702 每次有新政出台就会有一批人富起来! anger surprise
16703 [惊恐][愤怒] anger fear
16704 人生不止,寂寞不已。 sadness none
16705 寂寞人生爱无休,寂寞是爱永远的主题、我和我的影子独处、它说它有悄悄话想跟我说、它说它很想念你... sadness none

16706 rows × 3 columns


In [25]:
for elem in tree.iterfind('sentence[@emotion-2-type="none"]'):
    print(elem.tag, elem.text, elem.attrib)
list(tree.iterfind('sentence[@opinionated="N"]'))


Out[25]:
[]

In [60]:
tree1 = ET.ElementTree(file='/home/jeffmxh/temp.xml')

In [61]:
for elem in tree1.iter(tag='branch'):
    print(elem.tag, elem.attrib)


branch {'hash': '1cdf045c', 'name': 'codingpy.com'}
branch {'hash': 'f200013e', 'name': 'release01'}
branch {'name': 'invalid'}

In [83]:
for elem in tree1.iterfind('branch[@hash="1cdf045c"]'):
    print(elem.tag, elem.attrib)


branch {'hash': '1cdf045c', 'name': 'codingpy.com'}

In [5]:
writer = pd.ExcelWriter("sentiment_data.xlsx")
sentence_frame.to_excel(writer, sheet_name='sheet1', encoding='utf-8', index=False)
writer.save()