简体   繁体   English

从.txt提取数据

[英]Extract data from .txt

i'm doing a sudoku in python and i have a little problem to know how i am supposed to get data from a file 我正在用python做数独,我有一个小问题要知道我应该如何从文件中获取数据

so i have a file with who looks like this : 所以我有一个文件,看起来像这样:

.3. ... ...
..8 39. 6.. 
5.1 2.. 49.
.7. 6.. ...
2.. ... .4.
... 5.3 98.
... ... 15.
... ..7 ..9
4.. .1. 3..

every nine points it's a "\\n" 每九点就是一个“ \\ n”

How can i get the data? 我如何获取数据?

At the moment, i have just did that: 目前,我刚刚这样做:

import os

f = open('sudo.txt', 'r')
lines = f.read()
f.close()

sudoku = lines

print(sudoku)
os.system("pause")

Thanks for your help. 谢谢你的帮助。

This is actually much more difficult of a question than the other answers seem to be considering. 这个问题实际上比其他答案似乎要困难得多。 Especially solving the sudoku (which is outside the scope of this question) 尤其是解决数独问题(不在此问题范围内)

Consider what sort of a data structure you want. 考虑您想要哪种数据结构。 I'd imagine you'd want to repurpose a list in a custom class 我猜想您想在自定义类中重新使用list用途

class Puzzle(list):
    pass

and then pass it a list of each number left to right top to bottom, maybe using zero ( 0 ) as the blank space, meaning your constructor should be 然后将其从左到右从上到下的每个数字的列表传递给它,也许使用零( 0 )作为空格,这意味着您的构造函数应为

Puzzle([0,3,0,0,0,0,0,0,0,0,0,8,3,9,0,6,0,0, ... ])

How would you read this from your target data? 您将如何从目标数据中读取此信息? Well, one spot at a time. 好吧,一次一个地点。

with open("filename.txt") as inf:
    puzzle = [
        [0 if el == '.' else int(el) for el in line if el.strip()] 
        for line in inf]

That list comp is a bit of a mouthful though. 那个列表比较有点令人讨厌。 Let's go through it. 让我们经历一下。

puzzle = [ ... ]

Basic list comp syntax. 基本列表comp语法。 Nothing to talk about here 这里没什么好说的

[ [ 0 if el == '.' else int(el) for el in line if el.strip() ] ... ]

This is the mouthful, but it's basically just a NESTED list comp. 这是一口,但基本上只是一个嵌套列表。 This one says 这个说

[ 0 if el == '.'
# if the character is a period (.), use a zero (0)
else int(el)
# otherwise, cast the character to integer since it's a number
for el in line
# we're iterating over each character in the string line
if el.strip()
# and selecting every "truthy" character (e.g. skipping the spaces)

The rest of it is simple 其余的很简单

[ ... for line in inf]
# "line" is each literal line in the file.

Python loop constructs can basically iterate over anything, including strings, so for something like this, all you have to do is split the string on the newlines to separate the rows, and then iterate through those rows and put them into the array, if they aren't one of the spaces separating the boxes. Python循环构造基本上可以遍历任何事物,包括字符串,因此对于这样的事情,您要做的就是在换行符上拆分字符串以分隔行,然后遍历这些行并将它们放入数组中(如果它们)不是分隔盒子的空间之一。 So assuming you want the board in a 2d array, this list comprehension will do what you ask. 因此,假设您希望将木板放置在二维阵列中,则此列表理解将满足您的要求。

sudoku = [[character for character in line if not character==" "] for line in lines.split("\n")]

readlines will split the data on lines. readlines将在线上拆分数据。 Also, always use the with statement when opening files. 另外,打开文件时请始终使用with语句。

with open('sudo.txt') as f:
    lines = f.readlines()

You could then parse it further with split: 然后可以使用split进一步解析它:

soduku = [[list(group) for group in line.split()] for line in lines]

Which would put your data in this format: 这将使您的数据采用以下格式:

[[['.', '3', '.'], ['.', '.', '.'], ['.', '.', '.']],
 [['.', '.', '8'], ['3', '9', '.'], ['6', '.', '.']],
 [['5', '.', '1'], ['2', '.', '.'], ['4', '9', '.']],
 [['.', '7', '.'], ['6', '.', '.'], ['.', '.', '.']],
 [['2', '.', '.'], ['.', '.', '.'], ['.', '4', '.']],
 [['.', '.', '.'], ['5', '.', '3'], ['9', '8', '.']],
 [['.', '.', '.'], ['.', '.', '.'], ['1', '5', '.']],
 [['.', '.', '.'], ['.', '.', '7'], ['.', '.', '9']],
 [['4', '.', '.'], ['.', '1', '.'], ['3', '.', '.']]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM