简体   繁体   English

有没有办法在文本文件中读取arrays,但数组中的元素用分号分隔?

[英]Is there any way to read arrays in text file, but the elements in array are separated by semicolons?

I would like to read the array elements and assign them to the new array called data_array , but elements in the array in the text file are separated by semicolons.我想读取数组元素并将它们分配给名为data_array的新数组,但文本文件中数组中的元素用分号分隔。 How should I do if I want to read them normally as they are separated by commas?逗号隔开,想正常阅读怎么办? Furthermore, I want to read the line one by one.此外,我想逐行阅读。

Contents of file data.txt is as follows: data.txt文件内容如下:

4; -9; -1; 9; -1; -3; 8; 8; 0; -9; -8 
7; 6; 5; -6; 5; -4; 2; 1; -1; -6; 0; 3; -7; 9; 9; 6; -5; -8; 1; -8; -1; -1
-8; -6; -6; -5; -6; -8; -4; -2; 8; -3; 3; 6; 4; -9; 10; 2; -4; 7; -5; 0; -3; 7; -7; 6; -10; 8; -9; 9; 2; -1; -6; 6; -5; 8; 0; 3; 6; -10; -2; 8; -6; -5; 9; 10
1; 7; -6; 7; 6; 2; 9; 2; 3; 1; 0; 8; 5; 2; 4; 9; -10; 3; -9; 9; -2; 8; -1; 4; -4; -3; -10; -2; -9; -5; 7; 9; -4; 6; 10; -1; -8; 10; -2; 2; -9; 2; -2; 2; 3; 10; 6; 3; -3; -5; -4; 6; -4; -2; 2; -5; 4; -8; 0; -2; -5; -4; 3; 4; -6; -10; 3; -5; -10; -3; 4; 10; 10; 5; -5; 0; 10; 2; 9; 7; -8; -2; 10; 4; 10; 9; 3; -7; 
-3; -9; 5; -10; -3; 3; -7; 8; 8; 1; 8; -10; 0; -6; -10; 3; -10; 1; -1; -2; 10; 3; -3; -10; 9; -3; 9; 6; 2; 3; 6; -10; 1; -4; -1; 8; 5; 7; -6; -9; 1; -6; -9; 8; -7; -5; -4; -1; 10; 8; -10; -3; -10; -5; 1; 0; 5; -6; 7; 3; -8; -9; -8; -6; 3; 4; 0; 5; -9; 8; 7; -2; 0; -7; 7; 1; -2; 10; -7; 3; 10; -10; 5; 3; 3; -7; -3; -6; -3; -4; -6; 4; -1; 10; 7; 1; 5; 6; 0; -8; -6; -5; 6; 9; 2; 2; -8; 3; 2; -8; 1; -2; -10; 3; 8; 3; 6; 2; -5; 6; -8; -6; 10; -1; -7; 9; 3; -8; -9; 3; -2; 2; -9; -6; -2; -9; -4; 7; -6; 3; -5; 5; 4; 6; -7; 0; -4; 8; -9; 3; -1; -7; -9; 1; -5; -3; -2; 0; 4; 4; -3; -5; -8; -3; 0; -1; 5; -9; 5; 2; 4; 3; 3; 4; 10; -2

Here's my attempt:这是我的尝试:

f = open("data.txt", "r")
data_array = [f.readline()]

array_length = len(data_array)

The output is not what I expected, says the length of the array is only one. output 不是我所期望的,说数组的长度只有一个。

Is there any way that I can improve this much better?有什么办法可以更好地改善这一点吗?

Yes you can do it是的,你可以做到

import csv
result = []
with open('test.txt', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=';')
    for row in spamreader:
        result += [int(x) for x in row]

and result和结果

[4, -9, -1, 9, -1, -3, 8, 8, 0, -9, -8, 7, 6, 5, -6, 5, -4, 2, 1, -1, -6, 0, 3, -7, 9, 9, 6, -5, -8, 1, -8, -1, -1]

You could use a for-loop:您可以使用 for 循环:

f = open("data.txt", "r")
data_array = list()
for line in f.read():
    data_array.append(line.split(";"))

Then, it's possible to convert them to ints using a list comprehension:然后,可以使用列表理解将它们转换为整数:

for line in f.read():
    data_array.append(int(num) for num in line.split(";"))

This list comprehension reads the numeric values from the file and converts them to ints .列表理解从文件中读取数值并将它们转换为ints

Calling str.splitlines on the contents of the file produces output that's easier to work with than simply looping over the file.对文件内容调用str.splitlines会产生 output ,这比简单地遍历文件更容易处理。

filter (None, line.split(';')) gives us the result of line.split (';') with empty strings removed. filter (None, line.split(';')) 给我们line.split (';') 删除空字符串的结果。

data_array = [int(n) for line in f.read().splitlines()
              for n in filter(None, line.split(';'))]

It's the equivalent of this code:它等效于此代码:

data_array = []
for line in f.read().splitlines():
    for n in line.split(';'):
        n = n.strip()
        if n:
            data_array.append(int(n))

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

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