簡體   English   中英

逐行讀取txt文件,然后獲取每行的長度

[英]Reading txt file line by line, and then getting each line's length

data.txt 包含數組元素,以分號分隔。 每一行都應該被讀取為一個數組。

數據.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

這是我的第一次嘗試:

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

我完全被困住了。 我注意到分號也需要轉換為逗號。

我假設您想將每一行打包到一個整數列表(= 數組)中,並將所有這些數組收集到一個更大的容器中。 這是每個步驟解釋的版本:

# Use the context manager 'with' to make sure the file gets closed after
# you're done
with open('data.txt', 'r') as file:
    # Initialize container for the arrays
    arrays = []
    # Loop (iterate) over the lines of the file
    for line in file:
        # Remove whitespace at the ends (including \n = newline)
        line = line.strip()
        # Remove ';' from the end (at least in one line)
        line = line.rstrip(';')
        # Split the line on the ';'s into a list of strings representing
        # the numbers
        numbers_str = line.split(';')
        # Convert the list of string-numbers into a list (array) of integers
        array = [int(number_str) for number_str in numbers_str]
        # Append the newly created array to the array container
        arrays.append(array)
print(arrays)

一個更緊湊和“pythonic”的版本:

with open('data.txt', 'r') as file:
    arrays = [
        [int(number) for number in line.strip().rstrip(';').split(';')]
        for line in file.readlines()
    ]
print(arrays)

編輯:確定長度:

線長:

with open('data.txt', 'r') as file:
    for i, line in enumerate(file.readlines(), start=1):
        print(f'Length of line {i}: {len(line.rstrip())}')

整數數(在計算數組后運行):

for i, array in enumerate(arrays, start=1):
    print(f'Number of integers in line {i}: {len(array)}')

您當前的代碼將構建一個以整行作為唯一項的列表。 您必須拆分該行,然后將每個元素轉換為整數。 嘗試這個:

a = [int(s) for s in f.readline().split(";")]

要讀取所有行,您可以使用以下方法:

with open("data.txt", "r") as f:
    for line in f:
        a = [int(s) for s in line.split(";")]

這是找出每行長度的簡單方法。

arr = []
arr_len = []
with open('xyz.txt') as f:
    #iterate thru each line in the file
    for line in f:

        #for each line, strip the end of line and any spaces, then split by ';'
        x = line.strip('\n').strip().split(';')

        #store the contents into an array for future use
        arr.append([int(i) for i in x])

        #store the length of each line into an array
        arr_len.append(len(x))

print ('The number of elements in each line are:', arr_len)

輸出如下:

The number of elements in each line are: [11, 22, 44, 88, 176]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM