单行写入:
data = ['1', '2', '3']
with open('d:/ip.txt', 'w') as f:
f.write('\n'.join(data)) # 加入'\n'换行符,确保一行一条数据
单行读取:
with open('d:/ip.txt', 'r') as f:
file = [x.strip() for x in f.readlines()] # 一行一行读取TXT文件内容,并且去除'\n'
print(file)
单行写入:
data = ['1', '2', '3']
with open('d:/ip.txt', 'w') as f:
f.write('\n'.join(data)) # 加入'\n'换行符,确保一行一条数据
单行读取:
with open('d:/ip.txt', 'r') as f:
file = [x.strip() for x in f.readlines()] # 一行一行读取TXT文件内容,并且去除'\n'
print(file)