编写一个程序生成列表[11,22,33,44,55,66,77,88,99],将其写入文件python1.txt中,然后从文件中读出该列表,用print()函数输出
时间: 2024-05-09 19:18:16 浏览: 77
好的,现在我将编写一个程序来生成所需的列表并将其写入python1.txt文件中:
```
my_list = [11,22,33,44,55,66,77,88,99]
with open('python1.txt', 'w') as file:
for item in my_list:
file.write("%s\n" % item)
```
接下来,我将从文件中读取该列表并使用print()函数打印它:
```
with open('python1.txt', 'r') as file:
my_list = [int(line.strip()) for line in file]
print(my_list)
```
输出将是:
```
[11, 22, 33, 44, 55, 66, 77, 88, 99]
```
相关问题
用python编写一个程序生成列表[11,22,33,44,55,66,77,88,99],将其写入文件python1.txt中,然后从文件中读出该列表,用print()函数输出。
好的,以下是代码:
```python
# 生成列表并写入文件
my_list = [11,22,33,44,55,66,77,88,99]
with open('python1.txt', 'w') as f:
for num in my_list:
f.write(str(num) + '\n')
# 从文件中读出列表并输出
with open('python1.txt', 'r') as f:
my_list2 = []
for line in f.readlines():
my_list2.append(int(line.strip()))
print(my_list2)
```
这段代码会将列表写入文件`python1.txt`中,并且用`print()`函数输出从文件中读出来的列表。
阅读全文