从两个txt文件中生成json文件

时间:2019-04-27 14:06:44

标签: python json

我有两个不同的txt文件- 1)一个只有jpg文件 2)每个图像中的另一个带有五个阳离子,格式如下:

1000268201_693b08cb0e.jpg#0 A child in a pink dress is climbing up a set of stairs in an entry way .
1000268201_693b08cb0e.jpg#1 A girl going into a wooden building .
1000268201_693b08cb0e.jpg#2 A little girl climbing into a wooden playhouse .
1000268201_693b08cb0e.jpg#3 A little girl climbing the stairs to her playhouse .
1000268201_693b08cb0e.jpg#4 A little girl in a pink dress going into a wooden cabin .

其中0、1、2、3、4是同一张图片的字幕ID。

我想为所有数据创建类似的东西:-

{"images":[ {"imgurl": "static/img/667626_18933d713e.jpg", "description": "A girl is stretched out in shallow water", }, {"imgurl": "static/img/667626_18933d713e.jpg", "description": "A girl is stretched out in water", }, {"imgurl": "static/img/667626_18933d713e.jpg", "description": "description 3", }, {"imgurl": "static/img/667626_18933d713e.jpg", "description": "description 4", }, {"imgurl": "static/img/667626_18933d713e.jpg", "description": "description 5", } ] }

我正在寻找针对整个数据集立即执行此操作的python代码。 手动执行非常令人沮丧。

import glob

image_database = glob.glob('/static/img/img.jpg')
dataset_list = []
for image in image_database:
    image.show()
    print 'Enter the description':
    description = input()
    img_data = {}
    img_data['imgurl'] = image
    img_data['description'] = description
    dataset_list.append(img_data)
dataset_json = {}
dataset_json['images'] = dataset_list
json.dump(dataset_json, open('custom_dataset.json','wb')

我已将image.txt放在静态文件夹下,我目前正在做的是手动输入说明。

1 个答案:

答案 0 :(得分:0)

解析文件并生成json。

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var collectionView2: UICollectionView!
@IBOutlet weak var playerNameLabel: UILabel!
@IBOutlet weak var playerScoreLabel: UILabel!
@IBOutlet weak var oppNameLabel: UILabel!
@IBOutlet weak var oppScoreLabel: UILabel!
@IBOutlet weak var wordPlayLabel: UILabel!
@IBOutlet weak var endGameLabel: UILabel!
@IBOutlet weak var playButton: UIButton!
@IBOutlet var touchDrag: UILongPressGestureRecognizer!


override func viewDidLoad() {
    super.viewDidLoad()

    //Setting drag and drop delegates and labels for names (think is isn't correct though.)

    playerNameLabel.text = "\(game.playerName):"
    oppNameLabel.text = "\(game.opponentName):"


    self.collectionView.dragInteractionEnabled = true
    self.collectionView.dragDelegate = self
    self.collectionView.dropDelegate = self

    self.collectionView2.dragInteractionEnabled = true
    self.collectionView2.dragDelegate = self
    self.collectionView2.dropDelegate = self
    self.collectionView2.reorderingCadence = .fast

    //Tried to add this gesture to make it so you don't have to hold to drag but it doesn't work.

    touchDrag.minimumPressDuration = 0.0
    collectionView.addGestureRecognizer(touchDrag)
    collectionView2.addGestureRecognizer(touchDrag)    

    //play button is disabled by default. Only enabled when a valid word is placed.
    playButton.isEnabled = false
    dataSource2 = game.playerDataSource2