Python中文
首页
教程
问答
标签
搜索
登录
注册
无法让python写入文本文件,给了我一个空fi
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试图让我的程序写一个列表到一个文本文件。我把它改成了一个字符串,它创建了文本文件,但它是空的。我还得到它来打印变量,我试图写到文本文件,它出来完全一样,它应该。你知道吗</p> <pre><code>import string import re wholeLine = "" file = open("bowlingscores.txt", "r") for line in file: line = line.strip() wholeLine += line scores = re.findall('\d+', wholeLine) names = re.findall('\D+', wholeLine) file.close() scores = list(map(int,scores)) validScores = [x for x in scores if 300 >= x >= 0] average = sum(validScores) / len(validScores) numScores = len(scores) output = [] for i in range(numScores): if scores[i] == 300: output.append(names[i]) output.append("\nperfect") if scores[i] == average: output.append(names[i]) output.append("\naverage") if scores[i] < average: output.append(names[i]) output.append("\nbelow average") if scores[i] > average: if scores[i] <300: output.append(names[i]) output.append("\nabove average") if scores[i] > 300: output.append(names[i]) output.append("\ninvalid score") outputFile = open('bowlingaverages.txt', 'w') outputFile.write(str(output)) outputFile.close print(output) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>下面是如何使用输出(列表)写入文件:</p> <pre><code>with open('bowlingaverages.txt', 'w') as f: f.write(''.join(output)) print(output) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
我是否正确构建了这个递归神经网络
1 回答
我是否正确理解acquire和realease是如何在python库“线程化”中工作的
6 回答
我是否正确理解Keras中的批次大小?
2 回答
我是否正确理解PyTorch的加法和乘法?
3 回答
我是否正确组织了我的Django应用程序?
8 回答
我是否正确计算执行时间?如果是这样,那么并行处理将花费更长的时间。这看起来很奇怪
2 回答
我是否每次创建新项目时都必须在PyCharm中安装numpy?(安装而不是导入)
4 回答
我是否每次运行jupyter笔记本时都必须重新启动内核?
10 回答
我是否用python安装了socks模块?
9 回答
我是否真的需要知道超过一种语言,如果我想要制作网页应用程序?
9 回答
我是否缺少spaCy柠檬化中的预处理功能?
5 回答
我是否缺少给定状态下操作的检查?
1 回答
我是否能够使用函数“count()”来查找密码中大写字母的数量((Python)
1 回答
我是否能够使用用户输入作为colorama模块中的颜色?
3 回答
我是否能够创建一个能够添加新Django.contrib.auth公司没有登录到管理面板的用户?
1 回答
我是否能够将来自多个不同网站的数据合并到一个csv文件中?
4 回答
我是否能够将目录路径转换为可以输入python hdf5数据表的内容?
7 回答
我是否能够等到一个对象被销毁,直到它创建另一个对象,然后在循环中运行time.sleep()
8 回答
我是否能够通过CBV创建用户实例,而不是首先创建表单?(Django)
4 回答
我是否要使它成为递归函数?
8 回答