游戏存档创建器输出格式异常求助:消除多余空行
游戏存档创建器多余空行问题修复
我为自研游戏开发了一个存档创建器,但输出不符合预期。预期的game_users.txt格式如下:
default open open open open open open open open open 1
但实际保存的文件部分行之间存在多余空行,尝试多种方法仍无法修复,恳请帮忙解决。
原代码如下:
debug = False def profile_creator(): count2 = 0 u1 = 'Open' u2 = 'Open' u3 = 'Open' u4 = 'Open' u5 = 'Open' u6 = 'Open' u7 = 'Open' u8 = 'Open' u9 = 'Open' u10 = 'Open' content = ['HS:', '0', 'LS:', '0', 'G:', '0', '', 'LS = Last Score, HS = High Score, G = Games'] print('> Input Save Name') chosen_u = input('> ') chosen_u = str.lower(chosen_u) with open('game_info_' + chosen_u + '.txt', 'w') as f: for line in content: f.write(line) f.write('\n') f.close() with open('game_info_backup_' + chosen_u + '.txt', 'w') as f: for line in content: f.write(line) f.write('\n') f.close() with open('game_users.txt', 'r') as f: content = f.readlines() count = 1 for line in content: if count == 1: u1 = line if count == 2: u2 = line if count == 3: u3 = line if count == 4: u4 = line if count == 5: u5 = line if count == 6: u6 = line if count == 7: u7 = line if count == 8: u8 = line if count == 9: u9 = line if count == 10: u10 = line if count == 11: count2 = line count += 1 f.close() u1 = text_quality_control(u1) u2 = text_quality_control(u2) u3 = text_quality_control(u3) u4 = text_quality_control(u4) u5 = text_quality_control(u5) u6 = text_quality_control(u6) u7 = text_quality_control(u7) u8 = text_quality_control(u8) u9 = text_quality_control(u9) u10 = text_quality_control(u10) count2 = int(count2) count2 = str(count2) count2 = int(count2) count2 += 1 if count2 == 1: u1 = chosen_u if count2 == 2: u2 = chosen_u if count2 == 3: u3 = chosen_u if count2 == 4: u4 = chosen_u if count2 == 5: u5 = chosen_u if count2 == 6: u6 = chosen_u if count2 == 7: u7 = chosen_u if count2 == 8: u8 = chosen_u if count2 == 9: u9 = chosen_u if count2 == 10: u10 = chosen_u content = [u1, '', u2, '', u3, '', u4, u5, u6, u7, u8, u9, u10, str(count2)] with open('game_users.txt', 'w') as f: for line in content: f.write(line) f.write('\n') f.close() def text_quality_control(line): msg = str(line) stringer = '' indexer = 0 str_length = len(msg) for i in range(str_length - 1): msg2 = msg[indexer] msg2 = str.lower(msg2) if '1' in msg2: stringer = stringer + '1' if '2' in msg2: stringer = stringer + '2' if '3' in msg2: stringer = stringer + '3' if '4' in msg2: stringer = stringer + '4' if '5' in msg2: stringer = stringer + '5' if '6' in msg2: stringer = stringer + '6' if '7' in msg2: stringer = stringer + '7' if '8' in msg2: stringer = stringer + '8' if '9' in msg2: stringer = stringer + '9' if '0' in msg2: stringer = stringer + '0' if 'a' in msg2: stringer = stringer + 'a' if 'b' in msg2: stringer = stringer + 'b' if 'c' in msg2: stringer = stringer + 'c' if 'd' in msg2: stringer = stringer + 'd' if 'e' in msg2: stringer = stringer + 'e' if 'f' in msg2: stringer = stringer + 'f' if 'g' in msg2: stringer = stringer + 'g' if 'h' in msg2: stringer = stringer + 'h' if 'i' in msg2: stringer = stringer + 'i' if 'j' in msg2: stringer = stringer + 'j' if 'k' in msg2: stringer = stringer + 'k' if 'l' in msg2: stringer = stringer + 'l' if 'o' in msg2: stringer = stringer + 'o' if 'm' in msg2: stringer = stringer + 'm' if 'n' in msg2: stringer = stringer + 'n' if 'p' in msg2: stringer = stringer + 'p' if 'q' in msg2: stringer = stringer + 'q' if 'r' in msg2: stringer = stringer + 'r' if 's' in msg2: stringer = stringer + 's' if 't' in msg2: stringer = stringer + 't' if 'u' in msg2: stringer = stringer + 'u' if 'v' in msg2: stringer = stringer + 'v' if 'w' in msg2: stringer = stringer + 'w' if 'x' in msg2: stringer = stringer + 'x' if 'y' in msg2: stringer = stringer + 'y' if 'z' in msg2: stringer = stringer + 'z' if debug: print(stringer) indexer += 1 if debug: print(stringer) return stringer print('Input Save Name')
问题原因分析
- 读取
game_users.txt时使用readlines(),每行末尾会保留换行符\n,后续写入时又额外添加\n,导致重复换行出现空行。 - 构造写入
game_users.txt的content列表时,手动添加了空字符串'',写入时会生成多余空行,不符合预期格式。 - 原
text_quality_control函数逻辑冗余,且未彻底处理换行符问题。
修复后的代码
debug = False def profile_creator(): count2 = 0 # 用列表替代单独变量,简化结构 users = ['open'] * 10 content = ['HS:', '0', 'LS:', '0', 'G:', '0', '', 'LS = Last Score, HS = High Score, G = Games'] print('> Input Save Name') chosen_u = input('> ').lower() # 批量写入用户信息文件和备份 for filename in [f'game_info_{chosen_u}.txt', f'game_info_backup_{chosen_u}.txt']: with open(filename, 'w') as f: f.write('\n'.join(content)) # 读取并处理game_users.txt,去除换行符 try: with open('game_users.txt', 'r') as f: lines = [line.rstrip('\n') for line in f.readlines()] # 加载现有用户和计数 if len(lines) >= 11: users = lines[:10] count2 = int(lines[10]) except FileNotFoundError: # 文件不存在时初始化默认值 users = ['default'] + ['open'] * 9 count2 = 0 # 简化合法字符过滤逻辑 def text_quality_control(line): return ''.join([c for c in line.lower() if c in 'abcdefghijklmnopqrstuvwxyz0123456789']) # 清理所有用户内容 users = [text_quality_control(user) for user in users] count2 += 1 # 更新对应位置的用户 if 0 <= count2 - 1 < 10: users[count2 - 1] = text_quality_control(chosen_u) # 构造无多余空行的内容并写入 content_to_write = users + [str(count2)] with open('game_users.txt', 'w') as f: f.write('\n'.join(content_to_write)) print('Input Save Name') profile_creator()
修复说明
- 用列表
users替代单独的u1到u10变量,简化代码结构。 - 读取文件时用
rstrip('\n')去除每行末尾的换行符,避免重复添加导致空行。 - 移除原代码中手动添加的空字符串
'',确保写入内容无多余空行。 - 简化
text_quality_control函数,用列表推导式过滤合法字符,逻辑更高效简洁。 - 增加文件不存在的异常处理,自动初始化默认存档内容。
- 使用
'\n'.join(content)一次性写入所有内容,避免循环中多次写\n的问题。
内容的提问来源于stack exchange,提问作者Intergalactic Carpet
相关产品推荐
相关产品推荐

