【问题标题】:What to extract a value from object从对象中提取值的内容
【发布时间】:2022-01-11 07:27:34
【问题描述】:

我想要我的推文 json 中的媒体字段,但我无法获取它,而是获取了一些不必要的数据。 这是我正在使用的代码:

import csv
with open('csvdatafile.csv', 'r', encoding='utf-8', newline='') as fin, open('textdata.txt', 'w', encoding='utf-8', newline='') as fout:
    writer = csv.writer(fout, delimiter=' ')
    for row in csv.reader(fin):
        writer.writerow(row[23]);

我明白了

e x t e n d e d _ e n t i t i e s
{ ' m e d i a ' : " " [ { ' i d ' : " " 1 4 7 5 6 9 7 6 9 1 5 3 9 7 8 3 6 8 8 , " " ' i d _ s t r ' : " " ' 1 4 7 5 6 9 7 6 9 1 5 3 9 7 8 3 6 8 8 ' , " " ' i n d i c e s ' : " " [ 8 6 , " " 1 0 9 ] , " " ' m e d i a _ u r l ' : " " ' h t t p : / / p b s . t w i m g . c o m / m e d i a / F H q 7 O 1 w X w A g D 4 F g . j p g ' , " " ' m e d i a _ u r l _ h t t p s ' : " " ' h t t p s : / / p b s . t w i m g . c o m / m e d i a / F H q 7 O 1 w X w A g D 4 F g . j p g ' , " " ' u r l ' : " " ' h t t p s : / / t . c o / Y g o U X d R f 6 q ' , " " ' d i s p l a y _ u r l ' : " " ' p i c . t w i t t e r . c o m / Y g o U X d R f 6 q ' , " " ' e x p a n d e d _ u r l ' : " " ' h t t p s : / / t w i t t e r . c o m / K a t a n a H u g o / s t a t u s / 1 4 7 5 6 9 7 7 0 9 0 0 0 6 2 6 1 7 6 / p h o t o / 1 ' , " " ' t y p e ' : " " ' p h o t o ' , " " ' s i z e s ' : " " { ' t h u m b ' : " " { ' w ' : " " 1 5 0 , " " ' h ' : " " 1 5 0 , " " ' r e s i z e ' : " " ' c r o p ' } , " " ' l a r g e ' : " " { ' w ' : " " 7 2 0 , " " ' h ' : " " 8 9 9 , " " ' r e s i z e ' : " " ' f i t ' } , " " ' m e d i u m ' : " " { ' w ' : " " 7 2 0 , " " ' h ' : " " 8 9 9 , " " ' r e s i z e ' : " " ' f i t ' } , " " ' s m a l l ' : " " { ' w ' : " " 5 4 5 , " " ' h ' : " " 6 8 0 , " " ' r e s i z e ' : " " ' f i t ' } } , " " ' s o u r c e _ s t a t u s _ i d ' : " " 1 4 7 5 6 9 7 7 0 9 0 0 0 6 2 6 1 7 6 , " " ' s o u r c e _ s t a t u s _ i d _ s t r ' : " " ' 1 4 7 5 6 9 7 7 0 9 0 0 0 6 2 6 1 7 6 ' , " " ' s o u r c e _ u s e r _ i d ' : " " 7 1 8 8 6 3 3 2 8 0 6 0 2 8 9 0 2 5 , " " ' s o u r c e _ u s e r _ i d _ s t r ' : " " ' 7 1 8 8 6 3 3 2 8 0 6 0 2 8 9 0 2 5 ' } ] }

但我只想要 media_url

【问题讨论】:

  • 欢迎来到 SO.!很高兴看到你在这里找到了自己的路!您需要提供数据样本。否则很难解决您的问题。提供样品时,请勿张贴图片。而是将数据粘贴到问题中,就像您发布代码一样。

标签: python json


【解决方案1】:

试试这个。也许这可以帮助你。

import csv
    
with open('csvdatafile.csv', 'r', encoding='utf-8', newline='') as fin, open('textdata.txt', 'w', encoding='utf-8', newline='') as fout:
   reader = csv.reader(fin)
   next(reader)
   output = []
   for row in reader:
      output.append(row[:])
      writeURL = (str(output[0][4]).split("\'media_url\':\"\"\'"))[1].replace('\'','')
      fout.write(writeURL)

【讨论】:

  • 它显示列表索引超出范围我该怎么办?
  • 你能分享'csvdatafile.csv'文件的内容吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多