简体   繁体   English

将文本文件中的数据附加到python中的列表中

[英]appending data from a text file into a list in python

i am new to python and i need to read numbers from a text file and pass those in order into a list here is what i came up with : 我是python的新手,我需要从文本文件中读取数字并将这些数据按顺序传递到列表中,这是我想出的:

liste = []
with open("MDATX3.014") as fa:
    lines = fa.readlines()
    for line in lines:
        words = line.split()
        for word in words:
            liste.append(word)
            print(liste)

and i had the result like this 我有这样的结果 在此输入图像描述

You should print(liste) outside the loop if you don't want to see its intermediate values while it is being appended with new values: 如果在追加新值时不希望看到它的中间值,则应该在循环外print(liste)

liste = []
with open("MDATX3.014") as fa:
    lines = fa.readlines()
    for line in lines:
        words = line.split()
        for word in words:
            liste.append(word)
print(liste)

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

相关问题 如何 append 仅从给定起点将数据从文本文件到列表,而不是使用 python 附加完整文件 - How to append the data from a text file to a list only from a given starting point rather then appending full file using python 使用python将文本文件中的行附加到另一个文本文件 - Appending line from text file to another text file with python 使用python存储,检索,附加和编辑存储在文本文件中的数据 - Storing, retrieving, appending, and editing data stored in a text file with python 将元素从文本文件附加到字典并将它们设置为无 [Python] - Appending Elements to a Dictionary from a text file and Setting them to None [Python] 使用 Python 从文件复制文本后追加行 - Appending lines after copying text from a file using Python 使用python将数据附加到JSON文件? - Appending data to JSON file with python? 使用python将数据追加到JSON文件 - Appending data to JSON file with python 将项目从文件追加到列表 - Appending Items to a List from a File 没有分隔符的文本文件中的数据通过python进入列表 - Data from Text file without delimiter into list via python 将文本文件中的行附加到列表中时,会将整行设置为字符串。 如何识别文本文件中的不同数据类型? - When appending lines from a text file into a list, it sets the whole line to a string. How do I recognise different data types inside a text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM