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

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

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

问题

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

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

00x0x00 1

10x1x11 2

13x2x27 6

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

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

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

  1. textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
  2. print(textstring)
  3. thislist = textstring.replace(' ', ',')
  4. thatlist = thislist.split(',')
  5. print(thislist)
  6. print(thatlist)

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

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

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

  1. 00x0x00,1
  2. 10x1x11,2
  3. 13x2x27,6
  4. ['0\x000\x00x\x000\x00x\x000\x000\x00',
  5. '\x001\x00\r\x00\n\x00\r\x00\n\x001\x000\x00x\x001\x00x\x001\x001\x00',
  6. '\x002\x00\r\x00\n\x00\r\x00\n\x001\x003\x00x\x002\x00x\x002\x007\x00', '6\x00\x00\x00']

整个代码:

  1. import pygame
  2. SCREEN_WIDTH = 600
  3. SCREEN_HEIGHT = 400
  4. WHITE = (255, 255, 255)
  5. pygame.init()
  6. screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
  7. pygame.scrap.init()
  8. pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)
  9. thislist = ""
  10. textstring = ""
  11. thatlist = ""
  12. screen.fill(WHITE)
  13. running = True
  14. while running:
  15. for event in pygame.event.get():
  16. if event.type == pygame.QUIT:
  17. running = False
  18. elif event.type == pygame.KEYUP:
  19. if event.key == pygame.K_ESCAPE:
  20. running = False
  21. elif event.key == pygame.K_v and event.mod & pygame.KMOD_CTRL:
  22. textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
  23. print(textstring)
  24. thislist = textstring.replace(' ', ',')
  25. thatlist = thislist.split(',')
  26. print(thislist)
  27. print(thatlist)
  28. pygame.display.flip()
  29. 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...

  1. textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
  2. print(textstring)
  3. thislist = textstring.replace(' ', ',')
  4. thatlist = thislist.split(',')
  5. print(thislist)
  6. 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.

  1. 00x0x00,1
  2. 10x1x11,2
  3. 13x2x27,6
  4. ['0\x000\x00x\x000\x00x\x000\x000\x00',
  5. '\x001\x00\r\x00\n\x00\r\x00\n\x001\x000\x00x\x001\x00x\x001\x001\x00',
  6. '\x002\x00\r\x00\n\x00\r\x00\n\x001\x003\x00x\x002\x00x\x002\x007\x00', '\x006\x00\x00\x00']

Entire code:

  1. import pygame
  2. SCREEN_WIDTH = 600
  3. SCREEN_HEIGHT = 400
  4. WHITE = (255, 255, 255)
  5. pygame.init()
  6. screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
  7. pygame.scrap.init()
  8. pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)
  9. thislist = ""
  10. textstring = ""
  11. thatlist = ""
  12. screen.fill(WHITE)
  13. running = True
  14. while running:
  15. for event in pygame.event.get():
  16. if event.type == pygame.QUIT:
  17. running = False
  18. elif event.type == pygame.KEYUP:
  19. if event.key == pygame.K_ESCAPE:
  20. running = False
  21. elif event.key == pygame.K_v and event.mod & pygame.KMOD_CTRL:
  22. textstring = pygame.scrap.get("text/plain;charset=utf-8").decode()
  23. print(textstring)
  24. thislist = textstring.replace(' ', ',')
  25. thatlist = thislist.split(',')
  26. print(thislist)
  27. print(thatlist)
  28. pygame.display.flip()
  29. pygame.quit()

答案1

得分: 0

使用str而不是decode

  1. text = b'''00x0x00 1
  2. 10x1x11 2
  3. 13x2x27 6'''
  4. textstring = str(text, 'utf-8')
  5. print(textstring)
  6. thatlist = textstring.split()
  7. print(thatlist)

这是您想要的结果:

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

Use str instead of decode:

  1. text = b'''00x0x00 1
  2. 10x1x11 2
  3. 13x2x27 6'''
  4. textstring = str(text, 'utf-8')
  5. print(textstring)
  6. thatlist = textstring.split()
  7. print(thatlist)

And this is the result you want:

  1. 00x0x00 1
  2. 10x1x11 2
  3. 13x2x27 6
  4. ['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:

确定