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问题
jupyter运行一个旧的pytorch版本
10 回答
Jupyter运行不同版本的卸载库?
9 回答
Jupyter运行指定的键盘快捷键
3 回答
Jupyter通过.local文件“逃逸”virtualenv。我该如何缓解这种情况?
1 回答
Jupyter重新加载自定义样式
10 回答
Jupyter错误:“没有名为Jupyter_core.paths的模块”
6 回答
jupyter错误:无法在随机林中将决策树视为png
8 回答
Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块
2 回答
Jupyter错误地用阿拉伯语和字母数字元素显示Python列表
9 回答
Jupyter隐藏数据帧索引,但保留原始样式
6 回答
Jupyter集线器:启动器中出现致命错误。。。系统找不到指定的文件
1 回答
Jupyther中相同值的相同哈希,但导出到Bigquery时不相同
3 回答
Jupy上Python的读/写访问问题
8 回答
jupy上没有模块cv
1 回答
Jupy上的排序错误
4 回答
Jupy中bqplot图形的紧凑布局
5 回答
Jupy中matplotlib plot的连续更新
9 回答
Jupy中Numpy函数的文档
10 回答
Jupy中Pandas的自动完成问题
3 回答
jupy中Qt后端的Matplotlib动画
8 回答