获取TXT文件中的数字如何操作?

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

how to get numbers from a TXT file?

问题

你好,我是新手学习Python,我想从一个txt文件中读取一些数字作为二维点的向量。以下是要读取的.TXT文件内容:

map editor this is line 1
(700, 500)
(100, 500)
(700, 100)
(700, 100)

以下是用于读取的代码:

```python
import pygame
import keyboard

pygame.init()
# 定义窗口
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Map_editor")

_map = open('Map.txt', 'r+')
_points = _map.readlines()[1:]
print(_points[0])
point1 = _points[0]
point2 = _points[1]
# 更新基本操作
run = True
while run:
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
#=====================================================================================================================================================================
    # 代码在这里
    pygame.draw.line(window, (0, 255, 100), point1, point2, 10)
#=====================================================================================================================================================================
    pygame.display.update()
_map.close()
pygame.quit()

我得到了这个错误:File "C:\Users******\Desktop\AGame\Mapeditor.py", line 23, in pygame.draw.line(window, (0, 255, 100), point1, point2, 10)TypeError: invalid start_pos argument

我尝试添加int(_points[0]),但没有起作用。


<details>
<summary>英文:</summary>

hello im very new to python and **i want to read some numbers as a vector2 point from a txt file**
the .TXT file is :

map editor this is line 1
(700, 500)
(100, 500)
(700, 100)
(700, 100)


and the code that is supposed to read it is:

import pygame
import keyboard

pygame.init()
#define a window
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Map_editor")

_map = open('Map.txt', 'r+')
_points = _map.readlines()[1:]
print(_points[0])
point1 = _points[0]
point2 = _points[1]
#void update base
run = True
while run:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#=====================================================================================================================================================================
#code goes here
pygame.draw.line(window, (0, 255, 100), point1, point2, 10)

#=====================================================================================================================================================================
pygame.display.update()
_map.close()
pygame.quit()


i get this error:File "C:\Users******\Desktop\AGame\Mapeditor.py", line 23, in <module>pygame.draw.line(window, (0, 255, 100), point1, point2, 10)TypeError: invalid start_pos argument


i tried adding int(\_points\[0\]) but it did nothing

</details>


# 答案1
**得分**: 1

需要解析你的行。否则,你的 `readlines()` 将只包含每行作为字符串,而不是元组。

例如:

```python
_points[0]

这将只是字符串 &quot;(700, 500)&quot;,而不是元组 (700, 500)

这个问题 有答案展示了如何做到这一点。你需要在将它们作为 point1、point2 引用之前,对每行进行解析并将它们传递给你的绘图方法。

由于我没有你所有的依赖项,这里是关于你前几行的一些简要测试:

_map = open('Map.txt', 'r+')
_points = _map.readlines()[1:]
print(_points)

import re
_points = list(map(lambda row: tuple(map(int, re.findall(r'[0-9]+', row))), _points))
print(_points)

输出:

['(700, 500)\n', '(100, 500)\n', '(700, 100)\n', '(700, 100)']
[(700, 500), (100, 500), (700, 100), (700, 100)]

请注意,第一个 print 语句打印的是你拥有的内容,对应于输出的第一行,即字符串列表。第二个 print 语句打印了解析后的列表,因此实际上是元组的列表。

英文:

You need to parse your line. Otherwise, your readlines() will just contain each line as a string, instead of as tuples.

For example:

_points[0]

This will be just string &quot;(700, 500)&quot; instead of tuple (700, 500).

This question has answers that show how to do this. You will need to do this for each line before referring to them as point1, point2 and pass them into your drawing method.

Since I don't have all your dependencies, here are some brief tests about your first few lines:

_map = open(&#39;Map.txt&#39;, &#39;r+&#39;)
_points = _map.readlines()[1:]
print(_points)

import re
_points = list(map(lambda row: tuple(map(int, re.findall(r&#39;[0-9]+&#39;, row))), _points))
print(_points)

Output:

[&#39;(700, 500)\n&#39;, &#39;(100, 500)\n&#39;, &#39;(700, 100)\n&#39;, &#39;(700, 100)&#39;]
[(700, 500), (100, 500), (700, 100), (700, 100)]

Note that the first print statement prints what YOU have, which corresponds to the first line in the output, i.e., a list of strings. The second print statement prints the parsed list, so it is actually a list of tuples.

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

发表评论

匿名网友

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

确定