Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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_Python 3.x_List_File - Fatal编程技术网

Python 由列表组成的文本文件的串联?

Python 由列表组成的文本文件的串联?,python,python-3.x,list,file,Python,Python 3.x,List,File,假设我有两个文本文件1.txt和2.txt 1.txt的内容如下: [['Hi', 'I'], ['I'], ['_am']] [['_a', 'good'], ['boy']] 及 2.txt的内容如下: [['Hi', 'I'], ['I'], ['_am']] [['_a', 'good'], ['boy']] 如何以节省时间的方式将其加入并写入新文件,例如3.txt,如下所示: [['Hi', 'I'], ['I'], ['_am'], ['_a', 'good'], ['boy

假设我有两个文本文件1.txt和2.txt

1.txt的内容如下:

[['Hi', 'I'], ['I'], ['_am']]
[['_a', 'good'], ['boy']]

2.txt的内容如下:

[['Hi', 'I'], ['I'], ['_am']]
[['_a', 'good'], ['boy']]
如何以节省时间的方式将其加入并写入新文件,例如
3.txt
,如下所示:

[['Hi', 'I'], ['I'], ['_am'], ['_a', 'good'], ['boy']]
[['Hi', 'I'], ['I'], ['_am'], ['_a', 'good'], ['boy']]
注意:我希望特殊字符(u)保持原样

我尝试了前面关于堆栈溢出的一个答案,该答案在中提到

我的尝试如下:

global inputList
inputList = []
path = "F:/Try/"
def load_data():
    for file in ['1.txt', '2.txt']:
        with open(path + file, 'r', encoding = 'utf-8) as infile:
           inputList.extend(infile.readlines())
    print(inputList)
load_data()
["[['Hi', 'I'], ['I'], ['_am']]", "[['_a', 'good'], ['boy']]"]
但是我没有得到上面所示的期望输出。我现在得到的输出如下:

global inputList
inputList = []
path = "F:/Try/"
def load_data():
    for file in ['1.txt', '2.txt']:
        with open(path + file, 'r', encoding = 'utf-8) as infile:
           inputList.extend(infile.readlines())
    print(inputList)
load_data()
["[['Hi', 'I'], ['I'], ['_am']]", "[['_a', 'good'], ['boy']]"]
为什么我的电流输出中有一个额外的(“”)

请提出一些有成效的建议


提前谢谢。

您的文件总是这样吗?然后,只需删除“外部”
[
]

path = "F:/Try/"

def load_data():
    result = []
    for file in ['1.txt', '2.txt']:
        with open(path + file, 'r', encoding='utf-8') as infile:
            result.append(infile.readline().strip()[1:-1])
    return "[" + ", ".join(result) + "]"

print(load_data())
哪张照片

您可能需要使用:

import json

with open("1.txt", "r") as t1, open("2.txt", "r") as t2, open("3.txt", "w") as t3:
    t3.write(json.dumps([eval(t1.read().strip()), eval(t2.read().strip())]))

3.txt

[[["Hi", "I"], ["I"], ["_am"]], [["_a", "good"], ["boy"]]]

注:

输出:

[['Hi', 'I'], ['I'], ['_am']]
就你而言:

import ast
global inputList
inputList = []
path = "F:/Try/"
def load_data():
    for file in ['1.txt', '2.txt']:
        with open(path + file, 'r', encoding = 'utf-8') as infile:
           inputList.extend(ast.literal_eval(infile.readlines()))
    print(inputList)
load_data()
试试这个(别忘了放星形操作符):


您正在尝试从文件中读取数组。但是读取这样的文件会返回一个字符串。您必须解析字符串并将其转换为数组。这里出了很多问题。谢谢@TED。星形运算符是关于什么的?基本上,如果您有类似于[[a]]的输入,那么星形*会“展开”最外层的[],这样我们现在就有了作为[a]的输入。谢谢Pedro。我对json不是那么精通。我将通过你的建议链接。非常感谢!