使用Python字符串、列表和数据操作。

huangapple go评论78阅读模式
英文:

Working with Python strings, lists, and data (copy paste multilines)

问题

我有一些关于我尝试使用Python处理的文本格式的问题。

我有一个数据列表,格式如下:

00x0x00 1

10x1x11 2

13x2x27 6

我正在尝试将该列表复制粘贴到终端/ Pygame 窗口中,以便我可以处理它。

这部分我已经弄清楚,但我无法弄清楚:如何分离00x0x00和1?

我尝试使用str.split,但这样做时,我得到了一些“随机”的文本,而不是我试图获得的分割列表...

textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
print(textstring)

thislist = textstring.replace(' ', ',')
thatlist = thislist.split(',')
print(thislist)
print(thatlist)

用逗号替换空格完全正常,但我也无法使用逗号拆分字符串/列表。

我还尝试使用'/n' 进行拆分,效果与描述的相同。

这是当我使用上面写的列表运行以下代码时的输出。

00x0x00,1

10x1x11,2

13x2x27,6
['0\x000\x00x\x000\x00x\x000\x000\x00',
 '\x001\x00\r\x00\n\x00\r\x00\n\x001\x000\x00x\x001\x00x\x001\x001\x00',
 '\x002\x00\r\x00\n\x00\r\x00\n\x001\x003\x00x\x002\x00x\x002\x007\x00', '6\x00\x00\x00']

整个代码:

import pygame

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
WHITE = (255, 255, 255)
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
pygame.scrap.init()
pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)

thislist = ""
textstring = ""
thatlist = ""

screen.fill(WHITE)
running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

            elif event.key == pygame.K_v and event.mod & pygame.KMOD_CTRL:
                textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
                print(textstring)

                thislist = textstring.replace(' ', ',')
                thatlist = thislist.split(',')
                print(thislist)
                print(thatlist)

    pygame.display.flip()
pygame.quit()
英文:

I am having some problems with the text format I am trying to process with Python.

I have a list of data in this format:

00x0x00 1

10x1x11 2

13x2x27 6

I am trying copy paste that list into terminal/Pygame window so that I can process it.

This I have figured out, but I can’t figure out: How do I separate 00x0x00 and 1?

I have tried using str.split, but when I do so I get some "random" text, instead of the split list I'm trying to get...

            textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
            print(textstring)

            thislist = textstring.replace(' ', ',')
            thatlist = thislist.split(',')
            print(thislist)
            print(thatlist)

Replacing space with comma works just fine, but I can’t split the string/list with comma either.

I also tried splitting with '/n' too, with the same effect as described.

This is the output when I run the following with the list written above.

00x0x00,1

10x1x11,2

13x2x27,6
['0\x000\x00x\x000\x00x\x000\x000\x00',
'\x001\x00\r\x00\n\x00\r\x00\n\x001\x000\x00x\x001\x00x\x001\x001\x00',
'\x002\x00\r\x00\n\x00\r\x00\n\x001\x003\x00x\x002\x00x\x002\x007\x00', '\x006\x00\x00\x00']

Entire code:

import pygame

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
WHITE = (255, 255, 255)
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
pygame.scrap.init()
pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)

thislist = ""
textstring = ""
thatlist = ""

screen.fill(WHITE)
running = True
while running:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_ESCAPE:
            running = False

        elif event.key == pygame.K_v and event.mod & pygame.KMOD_CTRL:
            textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
            print(textstring)

            thislist = textstring.replace(' ', ',')
            thatlist = thislist.split(',')
            print(thislist)
            print(thatlist)

    pygame.display.flip()
pygame.quit()

答案1

得分: 0

使用str而不是decode

text = b'''00x0x00 1

10x1x11 2

13x2x27 6'''
textstring = str(text, 'utf-8')
print(textstring)

thatlist = textstring.split()
print(thatlist)

这是您想要的结果:

00x0x00 1

10x1x11 2

13x2x27 6
['00x0x00', '1', '10x1x11', '2', '13x2x27', '6']
英文:

Use str instead of decode:

text = b'''00x0x00 1

10x1x11 2

13x2x27 6'''
textstring = str(text, 'utf-8')
print(textstring)

thatlist = textstring.split()
print(thatlist)

And this is the result you want:

00x0x00 1

10x1x11 2

13x2x27 6
['00x0x00', '1', '10x1x11', '2', '13x2x27', '6']

huangapple
  • 本文由 发表于 2023年6月5日 15:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定