用逗号和标签附加每一行

时间:2017-03-02 14:27:01

标签: python

我在python中编写一个脚本来编写一个新行,然后用逗号和字符串标签添加该行。这是一段代码。

new = open(newpath + "/" + "train" + ".txt", 'w')

for idx, tr in enumerate(train_data):
    new.write(train_data[idx] + ' , '  + str(index)

输出如下

Recommendations, thoughts, tips and tricks, or peanuts on moving from wordpress to django
 , 1Wordpress [caption] processing
 , 1Wordpress: query all images in a posts media library

逗号和标签会附加到该行,但会将其移动到下一行。

我需要如下:

Recommendations, thoughts, tips and tricks, or peanuts on moving from wordpress to django , 1
Wordpress [caption] processing , 1
Wordpress: query all images in a posts media library , 1

1 个答案:

答案 0 :(得分:2)

根据您提供的内容,您的列车数据包含<script> import Dropzone from 'dropzone'; export default { data() { return { photos: [] }; }, ready() { Dropzone.autoDiscover = false; new Dropzone('form#upload-form', { url: '...', success: function(file, response) { this.photos = response.data.photos; // this.photos is not accessible here } }); } } </script> 个字符。因此,我建议您使用'\n'删除任何尾随空格和换行符。

str.strip

我不确定for idx, tr in enumerate(train_data): new.write(train_data[idx].strip() + ' , ' + str(index)) 来自哪里,所以我把它留在了。你也不必索引你的数据,因为你可以迭代它。

index