Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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中拆分txt中的项目?_Python_Python 3.x_List_Text - Fatal编程技术网

如何在python中拆分txt中的项目?

如何在python中拆分txt中的项目?,python,python-3.x,list,text,Python,Python 3.x,List,Text,我在文本文件中有以下代码: Host: 0.0.0.0 Port: 80 这是我的python代码: with open('config.txt', 'r') as configfile: lines = configfile.readlines() lines.split(': ') HOST = lines[1] PORT = lines[3] print(f'Your host is {HOST} and

我在文本文件中有以下代码:

Host: 0.0.0.0 Port: 80
                        
这是我的python代码:

with open('config.txt', 'r') as configfile:
    lines = configfile.readlines()
    lines.split(': ')
    HOST = lines[1]
    PORT = lines[3]

print(f'Your host is {HOST} and port is {PORT}')
但我得到了这个错误:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    lines.split(': ')
AttributeError: 'list' object has no attribute 'split'
回溯(最近一次呼叫最后一次):
文件“test.py”,第3行,在
行。拆分(“:”)
AttributeError:“list”对象没有属性“split”

我怎样才能解决这个问题?我对python相当陌生,这里有两个问题:

  • readlines()
    返回文件中所有行的列表
  • split
    返回一个列表,它不是就地操作
readlines()方法返回一个列表。不能对整个列表进行拆分

您应该按照以下几行操作,如果文本文件中没有多行,则可能不需要for循环。但是,您可能需要它来读取整个文本文件

with open('config.txt', 'r') as configfile:
    lines = configfile.readlines()
    for item in lines:
        item.split(': ')
        HOST = item[1]
        PORT = item[3]

        print(f'Your host is {HOST} and port is {PORT}')
如果需要索引不同的列表字符串,也可以使用range函数

with open('config.txt', 'r') as configfile:
    lines = configfile.readlines()
    for i in range(0,len(lines):
        split_string = lines[i].split(': ')
        HOST = split_string [1]
        PORT = split_string [3]

        print(f'Your host is {HOST} and port is {PORT}')

简单修复:

with open('config.txt', 'r') as configfile:
    lines = configfile.readline()
    lines = lines.split(': ')
    HOST = lines[1].split()[0]
    PORT = lines[2]

print("Your host is {HOST} and port is {PORT}".format(HOST = HOST, PORT = PORT))
  • 您需要使用
    readline()
    而不是
    readline()
  • 您需要在拆分后将lines变量重新分配为
    lines=lines.split(“:”)
  • 原因:

    with open('config.txt', 'r') as configfile:
        lines = configfile.readline()
        lines = lines.split(': ')
        HOST = lines[1].split()[0]
        PORT = lines[2]
    
    print("Your host is {HOST} and port is {PORT}".format(HOST = HOST, PORT = PORT))
    
  • readline()
    以字符串形式返回文件中的一行
  • readlines()
    以 字符串列表/数组
  • split方法不在原位,它返回 字符串列表,因此我们需要重新分配
    变量 使用返回的结果
  • 固定代码:

    with open('config.txt', 'r') as configfile:
        lines = configfile.readline()
        lines = lines.split(': ')
        HOST = lines[1].split()[0]
        PORT = lines[2]
    
    print("Your host is {HOST} and port is {PORT}".format(HOST = HOST, PORT = PORT))
    
    Config.txt:

    Host: 0.0.0.0 Port: 80
    
    输出:

    Your host is 0.0.0.0 and port is 80
    

    有两个问题,1)
    lines.split(“:”)
    ,使用
    lines=lines.split(“:”)
    返回一个新对象。2)
    readlines()
    返回所有行的列表。您可以对configfile.readlines()中的行使用
    或对configfile:中的行使用
    ,以逐个获取值。您希望的输出是什么?如果拆分该示例,(解决了您的另一个问题),您将得到
    “您的主机是0.0.0.0端口,端口是80”
    下次首次使用
    print()
    print(type(…)
    查看变量中的内容