在Python中加载文件

发布于 2025-01-05 05:44:51 字数 779 浏览 5 评论 0原文

我在使用以下代码时遇到一些问题:

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line:
            if character == "x":
                print "WALL"
            else:
                if character == "a":
                    print "LAND"
                else:
                    print "Unexpected Error loading map!"

townhall.map:

xxxxx
xaaax
xaaax
xaaax
xxxxx

我遇到的问题是它将换行符读取为字符;所以我在输出中得到这个 -

WALL
WALL
WALL
WALL
WALL
Unexpected Error loading map!
WALL
LAND
LAND
LAND
WALL
Unexpected Error loading map!
WALL
LAND
LAND
LAND
WALL
Unexpected Error loading map!
WALL
LAND
LAND
LAND
WALL
Unexpected Error loading map!
WALL
WALL
WALL
WALL
WALL

如何让它忽略换行“字符”?

I'm having some problems with the following code:

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line:
            if character == "x":
                print "WALL"
            else:
                if character == "a":
                    print "LAND"
                else:
                    print "Unexpected Error loading map!"

townhall.map:

xxxxx
xaaax
xaaax
xaaax
xxxxx

The problem I have is that it reads the newlines as characters; so I get this on output -

WALL
WALL
WALL
WALL
WALL
Unexpected Error loading map!
WALL
LAND
LAND
LAND
WALL
Unexpected Error loading map!
WALL
LAND
LAND
LAND
WALL
Unexpected Error loading map!
WALL
LAND
LAND
LAND
WALL
Unexpected Error loading map!
WALL
WALL
WALL
WALL
WALL

How do I make it ignore newline 'characters'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情独悲 2025-01-12 05:44:51

更改此行:

for character in line.rstrip():

您还可以使 if/else 结构更扁平:

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line.rstrip():
            if character == "x":
                print "WALL"
            elif character == "a":
                print "LAND"
            else:
                print "Unexpected Error loading map!"

或将打印定义为字典:

char = {'x': 'WALL',
        'a': 'LAND'}
with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line.rstrip():
            try:
                print char[character]
            except KeyError:
                print "Unexpected Error loading map!"

Change this line:

for character in line.rstrip():

And you can also make the if/else structure flatter:

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line.rstrip():
            if character == "x":
                print "WALL"
            elif character == "a":
                print "LAND"
            else:
                print "Unexpected Error loading map!"

or define the printing as a dictionary:

char = {'x': 'WALL',
        'a': 'LAND'}
with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line.rstrip():
            try:
                print char[character]
            except KeyError:
                print "Unexpected Error loading map!"
喵星人汪星人 2025-01-12 05:44:51

您还需要考虑换行符,并忽略它。

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line:
            if character == "x":
                print "WALL"
            elif character == "a":
                print "LAND"
            elif character == "\n":
                pass
            else:
                print "Unexpected Error loading map!"

You need to account for your newline character as well, and ignore it.

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line:
            if character == "x":
                print "WALL"
            elif character == "a":
                print "LAND"
            elif character == "\n":
                pass
            else:
                print "Unexpected Error loading map!"
你又不是我 2025-01-12 05:44:51

有很多方法可以做到这一点。其中之一是:

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line:
            if character in ['x','a']:
                if character == "x":
                    print "WALL"
                else:
                    if character == "a":
                        print "LAND"

There are many ways to do that. One of them is:

with open('townhall.map', 'r') as f:
    for line in f: 
        for character in line:
            if character in ['x','a']:
                if character == "x":
                    print "WALL"
                else:
                    if character == "a":
                        print "LAND"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文