Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python将文本文件读取到数组并编辑数组_Python_Arrays_Join_Add - Fatal编程技术网

python将文本文件读取到数组并编辑数组

python将文本文件读取到数组并编辑数组,python,arrays,join,add,Python,Arrays,Join,Add,您好,我正在尝试逐行读取文本文件,然后将所有数据存储到一个数组中,我想在数组的值中添加文本,例如 管理员 管理员 行政 日志 登录 在得到这行代码之后,我想添加.php 到头来 这是我的密码 current_folder= os.path.dirname(os.path.realpath(__file__)) current_list=str(current_folder)+"\pages.txt" ins = open( current_list, "r" ) array = [] for

您好,我正在尝试逐行读取文本文件,然后将所有数据存储到一个数组中,我想在数组的值中添加文本,例如

管理员 管理员 行政 日志 登录

在得到这行代码之后,我想添加.php 到头来

这是我的密码

current_folder= os.path.dirname(os.path.realpath(__file__))
current_list=str(current_folder)+"\pages.txt"

ins = open( current_list, "r" )
array = []
for line in ins:
    array.append(line.rstrip())

for fahad in array:

    array+".php"

您可以尝试以下代码:

ins = open( "hello.txt", "r" )
array = []
rows = ins.read().split('\n') #or \r\n - it depends from your txt
for row in rows:
    array.append(row+".php")

ins.close()
此代码:

try:
    with open('test.txt', 'r') as ins: #Opens the file and closes it when Python is done with it
        array = []
        for line in ins:
            array.append(line.rstrip()) # appends each line of the file with trailing white space stripped

        for fahad in array:
            fahad += ".php" # for each item in the list 'array' it concatenates '.php' on to the end. The += operator is the same as fahad = fahad + '.php'
            print(fahad)

except FileNotFoundError: # this is part of a try/except block. If the file isn't found instead of throwing an error this will trigger. Right now nothing happens because of the pass statement but you can change that to print something if you like.
    pass
产生:

>>> fahad
'admin administrator adm log login.php'

我想这应该行得通

current_folder= os.path.dirname(os.path.realpath(__file__))
current_list=str(current_folder)+"\pages.txt"

ins = open( current_list, "r" ).read().split()
array = []
for line in ins:
    array.append(line + ".php")

您是希望文件中的每个值,还是希望整行作为一个条目?例如,在上次的lop中,数组=['admin'、'administrator'、'adm'、'log'、'login']或数组=['administrator adm log login']存在错误。将主体中的数组替换为fahad,它应该按照您希望的方式运行。看起来您只是在谈论一个列表。在Python中,数组和列表表示两种不同的含义。特别是,数组通常是同构集合。