简体   繁体   English

从 2 个不同的 arrays 中的 txt 文件中拆分行的字符串

[英]Split lines's strings from a txt file in 2 different arrays

I have a text file with x and y coordinates.我有一个带有 x 和 y 坐标的文本文件。 I am trying to store the coordinates to x and y arrays.我正在尝试将坐标存储到 x 和 y arrays。 This is the file I want to store.这是我要存储的文件。

100 511
52 502
384 94
46 506
54 508
399 101
394 93

I expect to be like this我希望是这样的

x[0] = 100, y[0] = 511;
x[1] = 52, y[1] =502

and so on等等

for n in range(0,lines[0].find(' ')):
    i = 0
    x[i] = x[i] + n
    i = i + 1

I have tried something like this to find the 'space' but it didn't work.我曾尝试过类似的方法来找到“空间”,但没有奏效。 Does anyone have an idea?有人有想法吗?

Try using:尝试使用:

x, y = zip(*[i.split() for i in lines])

Raw answer:原始答案:

x = []
y = []

with open(file_path) as f:
    for line in f:
        x_coord, y_coord = line.split()
        x.append(x_coord)
        y.append(y_coord)

Try this尝试这个

num_list = [line.strip('\n').split() for line in open('numbers.txt').readlines()]
x, y = zip(*num_list)
print x, y

Result: ('100', '52', '384', '46', '54', '399', '394') ('511', '502', '94', '506', '508', '101', '93')

Read number.txt and zip it.阅读 number.txt 和 zip 它。

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

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