我想从用户那里获取包含字符串的输入,将该字符串存储到变量中,然后将其写入另一个字符串行中。
我尝试使用open和.write并使用(%s)将输入添加到写入的行中。
outF = open("file.txt", "w")
string = raw_input("enter string to add!")
outF.write('string data to write (%s)', % string)
outF.close()
答案 0 :(得分:0)
尝试一下:
outF = open("file.txt", "a") #append mode if u wish to add more data to the file later
string = raw_input("enter string to add!")
string_new="string u want to add before"+string #add strings wherever u need
outF.write(string_new)
outF.close()
答案 1 :(得分:0)
看看这是否适合您
export class Utils {
pushUnique<T, U>(arr: T[], obj: T, key: (o: T) => U = null, logVerbose: boolean = false): void {
if (logVerbose === true) {
console.log('pushUnique called');
}
if (typeof obj === 'object' && key === null) {
console.warn('Object defined in pushUnique is complex, but a key was not specified.');
} else if (typeof obj !== 'object' && key !== null) {
console.warn('Object is not complex, but a key was specified');
}
const index = key !== null ? arr.findIndex(o => key(o) === key(o)) : arr.indexOf(obj);
if (index === -1) {
arr.push(obj);
} else {
if (logVerbose === true) {
console.log('Duplicate object, not added');
}
}
}
}