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

Python 通过读取文本文件创建列表列表

Python 通过读取文本文件创建列表列表,python,text,readfile,pathname,Python,Text,Readfile,Pathname,所以我正试图自动化一项繁琐的任务 我有一个test.txt,它总结了一些pdf文件的文件路径 "L:\Advertentie woningplattegronden\Definitieve plattegronden\Gemeente Delft\Complex 1004\Copy\1004A0Oa00 Jacob Gillishof 10.pdf" "L:\Advertentie woningplattegronden\Definitieve plattegronden\Gemeente D

所以我正试图自动化一项繁琐的任务

我有一个test.txt,它总结了一些pdf文件的文件路径

 "L:\Advertentie woningplattegronden\Definitieve plattegronden\Gemeente Delft\Complex 1004\Copy\1004A0Oa00 Jacob Gillishof 10.pdf"
"L:\Advertentie woningplattegronden\Definitieve plattegronden\Gemeente Delft\Complex 1004\Copy\1004A0Oa00 Jacob Gillishof 11.pdf"
"L:\Advertentie woningplattegronden\Definitieve plattegronden\Gemeente Delft\Complex 1004\Copy\1004A0Oa00 Jacob Gillishof 14.pdf"
我需要脚本为步骤1做的是列出我使用的每一行:

with open('Test.txt') as f:
textlines = f.read().splitlines()
print(textlines)
其结果是:

[
    '"L:\\Advertentie woningplattegronden\\Definitieve plattegronden\\Gemeente Delft\\Complex 1004\\Copy\\1004A0Oa00 Jacob Gillishof 10.pdf"',
    '"L:\\Advertentie woningplattegronden\\Definitieve plattegronden\\Gemeente Delft\\Complex 1004\\Copy\\1004A0Oa00 Jacob Gillishof 11.pdf"',
    '"L:\\Advertentie woningplattegronden\\Definitieve plattegronden\\Gemeente Delft\\Complex 1004\\Copy\\1004A0Oa00 Jacob Gillishof 14.pdf"',
    "",
    "",
]
但不确定为什么最后两个对象是空字符串

然后我想创建另一个列表,该列表在textlines列表中循环,并分隔路径\中的所有内容

所以我想要一个包含以下内容的列表:

some_list = [
    "L:",
    "Advertentie woningplattegronden",
    "Definitieve plattegronden",
    "Gemeente Delft",
    "Complex 1004",
    "Copy",
    "1004A0Oa00 Jacob Gillishof 10.pdf",
]
最后,我希望能够将一些索引从某个_列表中放入一个新变量,以便稍后创建一个包含这些变量的文件(csv)

每次我尝试循环第一个列表时,都会收到一个错误,告诉我字符串索引超出范围

我不是要求一个完整的脚本顺便说一句,但一些指导将是很好的如何继续这个脚本

提前谢谢

您可以尝试使用.split(“\”)


也许是这样的?我在这里和那里加了一些有用的评论

filenames = []

with open("file.txt", "r") as file:
    for line in file:
        line = line.strip()  # remove any trailing/leading spaces
        line = line.strip('"')  # remove wrapping quotes
        if line:  # if there still is content...
            filenames.append(line)  # save the valid line.

filename_components = [
    filename.split("\\")  # Split the filename by backslashes
    for filename in filenames  # for each filename  # in the filenames we just stored
]

for split_name in filename_components:
    print(split_name)  # print out each split name
输出,例如

['L:', 'Advertentie woningplattegronden', 'Definitieve plattegronden', 'Gemeente Delft', 'Complex 1004', 'Copy', '1004A0Oa00 Jacob Gillishof 10.pdf']
['L:', 'Advertentie woningplattegronden', 'Definitieve plattegronden', 'Gemeente Delft', 'Complex 1004', 'Copy', '1004A0Oa00 Jacob Gillishof 11.pdf']
['L:', 'Advertentie woningplattegronden', 'Definitieve plattegronden', 'Gemeente Delft', 'Complex 1004', 'Copy', '1004A0Oa00 Jacob Gillishof 14.pdf']

首先,您需要稍微清理一下输入。这些空字符串可能是文件末尾的空行,因此您必须忽略它们。另外,请注意,您的行用双引号括起来,这可能不是您想要的。您可以使用
.strip(“”)


最后,我猜
索引器可能是因为试图在空行中找到反斜杠,这让我觉得您是在手动搜索它们,而不是使用split。正如@Bernd所说,使用
.split(\\)
在每一行上都会将字符串切割成您想要的所有部分,并返回一个列表。

您需要避开反斜杠。首先,请使用拆分(\\”),非常感谢您的快速响应和澄清注释!
['L:', 'Advertentie woningplattegronden', 'Definitieve plattegronden', 'Gemeente Delft', 'Complex 1004', 'Copy', '1004A0Oa00 Jacob Gillishof 10.pdf']
['L:', 'Advertentie woningplattegronden', 'Definitieve plattegronden', 'Gemeente Delft', 'Complex 1004', 'Copy', '1004A0Oa00 Jacob Gillishof 11.pdf']
['L:', 'Advertentie woningplattegronden', 'Definitieve plattegronden', 'Gemeente Delft', 'Complex 1004', 'Copy', '1004A0Oa00 Jacob Gillishof 14.pdf']