怎么用python将列表写入文件_使用整数Python从列表中写入文件(Write to a file from a list with integer Python)...

Im trying to write a text file from a list, it works perfectly with a list of strings, each element gets on a new line. But when I try to write from a list with integers it does not work. The thing is I need that list as an integer-list in another place in the code (when comparing values in an if-statement), and I've tried to convert the integer-list to a string-list, but then the numbers gets placed on separate lines like for e.g.,

I have to write 2016 in one line:

2016

but, it gets like this

2

0

1

6

here is the code for writing to a text file:

print("write year: ") year.append(int(input()))

...some code that needs the list year as an integer...

year_string = str(year)

with open('movietitle.txt', 'w') as file:

file.write('\n'.join(movietitle))

with open('year_string.txt', 'w') as file:

file.write('\n'.join(year_string))

with open('genre.txt', 'w') as file:

file.write('\n'.join(genre))

with open('review.txt', 'w') as file:

file.write('\n'.join(review))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值