0

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. 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:

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.

Is there any way that I can improve this much better?

Heikki
  • 2,214
  • 19
  • 34
  • 2
    you could maybe use [csv](https://docs.python.org/3/library/csv.html) module from the stdlib – han solo Sep 22 '20 at 06:09
  • `a=splitlines()` to split by lines. `for line in a` to loop line by line and `line.split(';')` to split by this delimiter. The same as in this answer https://stackoverflow.com/questions/16922214/reading-a-text-file-and-splitting-it-into-single-words-in-python/16922328 – Yossi Levi Sep 22 '20 at 06:13
  • Does this answer your question? [pyparsing parsing csv files with semi-colon instead of comma](https://stackoverflow.com/questions/19514602/pyparsing-parsing-csv-files-with-semi-colon-instead-of-comma) – Brian McCutchon Sep 22 '20 at 06:14

3 Answers3

1

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]
0

You could use a for-loop:

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(";"))
Banana
  • 2,295
  • 1
  • 8
  • 25
0

This list comprehension reads the numeric values from the file and converts them to ints.

Calling str.splitlines on the contents of the file produces output that's easier to work with than simply looping over the file.

filter(None, line.split(';')) gives us the result of line.split(';') with empty strings removed.

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))
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153