示例:
布拉姆拉
布拉姆拉
布拉姆拉
转向:
爆破,爆破,爆破
with open('a.txt', 'r') as f:
temp = f.readlines()
for line in temp:
line.replace('\n', ', ', end='')
print(temp)
input()发布于 2021-08-04 16:43:42
使用read()比使用readlines()更适合您的情况。您可以执行以下操作:
with open('a.txt', 'r') as f:
temp = f.read().replace("\n", ",")
print(temp)编辑:如果最后有一个新行,上面的代码将给出输出如下:
blahblahblah,blahblahblah,blahblahblah,
为了删除最后一个逗号,只需使用temp[:-1]
https://stackoverflow.com/questions/68654860
复制相似问题