我写了以下python代码。我期望它做的是将文件“noise”中的一个随机单词添加到“raw”的每一行并将其打印到文件“dataset”
#! /usr/bin/python
from random import randint
raw = open("raw_dataset_1", "r")
noise = open("random", "r")
dataset = open("raw_noisy", "w")
lines = noise.readlines()
for line in raw:
a = randint(1, 5449)
addNoise = lines[a-1]
#print a
#print addNoise
noisy = (line + addNoise)
noisy1= noisy.rstrip()
#print noisy1
dataset.write(noisy1)
我预期的“数据集”文件是:
city mountain sky sun chalk
bay lake sun tree discussions
beach sea sky sun background
但我得到:
city mountain sky sun
chalk
bay lake sun tree
discussions
beach sea sky sun
background
有人可以指出我的错误吗?