如何在 python 中使用 split 函数来拆分部分文本并将它们保存到不同的文件?

尼古拉斯·阿吉鲁

您好,我在 python 中使用 split 函数时遇到问题,但没有成功。我使用爬虫收集了一些推文,我需要将每条推文的某些部分拆分为不同的 .json 文件,特别是 ID 和 #(hashtag)。我一直在使用 split 功能没有成功我做错了什么?我想将“id”和“text”之后
的内容保存到不同的 .json 文件中,文本如下所示:

{"created_at":"Fri Oct 20 16:35:36 +0000 2017","id":921414607302025216,"id_str":"921414607302025216","text":"@IdrisAhmed1 间接你说?

def on_data(self, data):
    try:
        #print data
        with open('Bologna_streams.json', 'r') as f:
            for line in f:

                tweet = data.spit(',"text":"')[1].split('",""source"')[0]
                print (tweet)

                saveThis = str(time.time()) + '::' +tweet

                saveFile = open('Bologna_text_preprocessing.json', 'w')
                json.dump(data)
                saveFile.write(saveThis)
                saveFile.write(tweet)
                saveFile.write('\n')
                saveFile.close()
                f.close()
        return True
    except BaseException as e:
        print("Error on_data: %s" % str(e))
        time.sleep(5)

def on_error(self, status):
    print (status)
安德鲁·E

我认为您应该在命令行上以交互方式或在小脚本中尝试使用 Python。

考虑一下:

text="""
{"created_at":"Fri Oct 20 16:35:36 +0000 2017","id":921414607302025216,"id_str":"921414607302025216","text":"@IdrisAhmed16 learn #python"}
""".strip()

print(text.split(":"))

这将在控制台中打印:

['{"created_at"', '"Fri Oct 20 16', '35', '36 +0000 2017","id"', '921414607302025216,"id_str"', '"921414607302025216","text"', '"@IdrisAhmed16 learn #python"}']

或者,要在新行上打印每个分屏:

print("splits:\n")
for item in text.split(":"):
  print(item)
print("\n---")

这将打印:

splits:

{"created_at"
"Fri Oct 20 16
35
36 +0000 2017","id"
921414607302025216,"id_str"
"921414607302025216","text"
"@IdrisAhmed16 #learn python"}

---

换句话说,split已经完成了它应该做的事情:找到每个":"并围绕这些字符拆分字符串。

您要做的是解析 JSON:

import json

parsed = json.loads(text)
print("parsed:", parsed)

parsed变量是一个普通的 Python 对象。结果:

parsed: {
  'created_at': 'Fri Oct 20 16:35:36 +0000 2017',
  'id': 921414607302025216,
  'id_str': '921414607302025216',
  'text': '@IdrisAhmed16 learn #python'
}

现在您可以对数据进行操作,包括检索text项目和拆分项目。

但是,如果目标是找到所有主题标签,则最好使用正则表达式:

import re
hashtag_pattern = re.compile('#(\w+)')
matches = hashtag_pattern.findall(parsed['text'])
print("All hashtags in tweet:", matches)

print("Another example:", hashtag_pattern.findall("ok #learn #python #stackoverflow!"))

结果:

All hashtags in tweet: ['python']
Another example: ['learn', 'python', 'stackoverflow']

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在python中使用.split方法?

来自分类Dev

如何在python中使用shell命令拆分文件

来自分类Dev

如何在函数中使用.get()。Python

来自分类Dev

如何在Python中使用round()函数?

来自分类Dev

如何在python中使用随机函数?

来自分类Dev

如何在Python中使用参数、函数

来自分类Dev

如何在python中使用文件路径?

来自分类Dev

如何在angularjs中使用split

来自分类Dev

如何在angularjs中使用split

来自分类Dev

如何在python中使用split()提取最后两个单词?

来自分类Dev

如何在Python 2.6中使用.split()从输出生成数组/列表

来自分类Dev

如何在python文件中使用外部代码python

来自分类Dev

如何在使用Python的hadoop流作业中使用文件?

来自分类Dev

如何在C#中使用Split()方法获取n部分的String?

来自分类Dev

如何在javascript中使用split函数和RegEx按长度分割字符串?

来自分类Dev

如何在Python中使用递归拆分字符串?

来自分类Dev

如何在Python数据框中的每一行上使用split函数?

来自分类Dev

如何在Python中的不同类中使用变量?

来自分类Dev

如何在Python中使用相同的方法重构不同的类?

来自分类Dev

如何在不同版本的python中使用scrapy

来自分类Dev

如何在Python中使用保存模型进行预测

来自分类Dev

如何在Python中使用Selenium WebDriver获取文本

来自分类Dev

如何在Python中使用Selenium Webdriver提取文本?

来自分类Dev

如何在Python中使用Beautifulsoup仅打印文本?

来自分类Dev

如何在Python中使用OpenCV检测行上方的文本

来自分类Dev

如何在Python中使用Beautifulsoup获取嵌套标签的文本?

来自分类Dev

如何在Python类中使用模块函数

来自分类Dev

如何在python中使用用户定义的函数?

来自分类Dev

如何在python中使用类函数隐藏按钮

Related 相关文章

  1. 1

    如何在python中使用.split方法?

  2. 2

    如何在python中使用shell命令拆分文件

  3. 3

    如何在函数中使用.get()。Python

  4. 4

    如何在Python中使用round()函数?

  5. 5

    如何在python中使用随机函数?

  6. 6

    如何在Python中使用参数、函数

  7. 7

    如何在python中使用文件路径?

  8. 8

    如何在angularjs中使用split

  9. 9

    如何在angularjs中使用split

  10. 10

    如何在python中使用split()提取最后两个单词?

  11. 11

    如何在Python 2.6中使用.split()从输出生成数组/列表

  12. 12

    如何在python文件中使用外部代码python

  13. 13

    如何在使用Python的hadoop流作业中使用文件?

  14. 14

    如何在C#中使用Split()方法获取n部分的String?

  15. 15

    如何在javascript中使用split函数和RegEx按长度分割字符串?

  16. 16

    如何在Python中使用递归拆分字符串?

  17. 17

    如何在Python数据框中的每一行上使用split函数?

  18. 18

    如何在Python中的不同类中使用变量?

  19. 19

    如何在Python中使用相同的方法重构不同的类?

  20. 20

    如何在不同版本的python中使用scrapy

  21. 21

    如何在Python中使用保存模型进行预测

  22. 22

    如何在Python中使用Selenium WebDriver获取文本

  23. 23

    如何在Python中使用Selenium Webdriver提取文本?

  24. 24

    如何在Python中使用Beautifulsoup仅打印文本?

  25. 25

    如何在Python中使用OpenCV检测行上方的文本

  26. 26

    如何在Python中使用Beautifulsoup获取嵌套标签的文本?

  27. 27

    如何在Python类中使用模块函数

  28. 28

    如何在python中使用用户定义的函数?

  29. 29

    如何在python中使用类函数隐藏按钮

热门标签

归档