简体   繁体   English

为什么 read().split("\n") 会导致错误,即使它从列表中删除了 "\n" 不像 readlines() 和 readlines() 工作

[英]why does read().split("\n") result in an error even though it removes the "\n" from the list unlike readlines() and readlines() works

File1 contains the following integers: File1 包含以下整数:

3
6
5
8
33
12
7
4
72
2
42
13

File2 contains the following integers: File2 包含以下整数:

3
6
13
5
7
89
12
3
33
34
1
344
42

Error for read()split("n") read()split("n") 出错

    Traceback (most recent call last):
      File "main.py", line 12, in <module>
        result = [int(number) for number in list1 if number in list2]
      File "main.py", line 12, in <listcomp>
        result = [int(number) for number in list1 if number in list2]
    ValueError: invalid literal for int() with base 10: ''

Output for: Question: Why does this section of code work when the other does not? Output for: 问题:为什么这段代码可以工作,而其他代码不能工作? I figured I had to removed the "\n" from the newly created strings in the list1 and list2 before it could be converted into an integer using the list comprehension, which is why I tried the read().split("\n") code section first to remove the escape character.我想我必须从 list1 和 list2 中新创建的字符串中删除“\n”,然后才能使用列表理解将其转换为 integer,这就是我尝试 read().split("\n" 的原因) 代码部分首先删除转义字符。 The readlines() section of code is able to convert the strings containing "\n" no problem.代码的 readlines() 部分能够转换包含“\n”的字符串没有问题。 Is this because when using the int() escape characters are ignored?这是因为在使用 int() 转义字符时会被忽略吗?


    with open("file1.txt","r") as file1:
          #list1 = file1.readlines()
    with open("file2.txt","r") as file2:
          list2 = file2.readlines()

['3\n', '6\n', '5\n', '8\n', '33\n', '12\n', '7\n', '4\n', '72\n', '2\n', '42\n', '13\n']

['3\n', '6\n', '13\n', '5\n', '7\n', '89\n', '12\n', '3\n', '33\n', '34\n', '1\n', '344\n', '42\n']

[3, 6, 5, 33, 12, 7, 42, 13]

Python: Python:

with open("file1.txt","r") as file1:
    list1 = file1.read().split("\n")
with open("file2.txt","r") as file2:
    list2 = file2.read().split("\n")

# with open("file1.txt","r") as file1:
      #list1 = file1.readlines()

# with open("file2.txt","r") as file2:
      #list2 = file2.readlines()

result = [int(number) for number in list1 if number in list2]

print(result)

It's a little unclear what you are asking but I'll do my best to answer why these two lines are not equivalent and why the second one leads to a ValueError .有点不清楚你在问什么,但我会尽力回答为什么这两行不等价以及为什么第二行会导致ValueError

  • file1.readlines()
  • file1.read().split("\n")

file1.readlines() file1.readlines()

This one is simple.这个很简单。 The readlines method simply converts a file into a list of strings. readlines方法只是将文件转换为字符串列表。 One line in the file corresponds to one item in the list.文件中的一行对应于列表中的一项。 There are 12 rows in the file so you get a list of 12 strings.文件中有 12 行,因此您会得到一个包含 12 个字符串的列表。

with open("file1.txt", "r") as file1:
    print(file1.readlines())

# ['3', '6', '5', '8', '33', '12', '7', '4', '72', '2', '42', '13']

file1.read().split("\n") file1.read().split("\n")

The read method returns the entire file as a string, in this case.在这种情况下, read方法将整个文件作为字符串返回。 This also includes newline characters as you noted in the original post.这还包括您在原始帖子中提到的换行符。

with open("file1.txt", "r") as file1:
    file1.read()

# '3\n6\n5\n8\n33\n12\n7\n4\n72\n2\n42\n13\n'

Now when you use the split method on a string, it Python splits the string at instance of the delimiter and returns all pieces of the string.现在,当您对字符串使用split方法时,它 Python 在分隔符的实例处拆分字符串并返回字符串的所有片段。 For example, if I say "123".split("2") then I can expect ['1', '3'] to be returned.例如,如果我说"123".split("2")那么我可以期望['1', '3']被返回。 However, it's less intuitive what happens when the specified delimiter is at the end of the string.但是,当指定的分隔符位于字符串的末尾时,会发生什么不太直观。 If I say "123".split("3") then I can expect ['12', ''] .如果我说"123".split("3")那么我可以期待['12', ''] So if the delimiter is at the end or beginning of a string, split will return an empty string at the end or beginning of the list.因此,如果分隔符位于字符串的末尾或开头, split将在列表的末尾或开头返回一个空字符串。

In the case of file1 , there is a \n delimiter at the end of the string so you can expect something like this when split:file1的情况下,字符串末尾有一个\n分隔符,因此在拆分时您可以期待这样的内容:

with open("file1.txt", "r") as file1:
    file1.read().split('\n')

# ['3', '6', '5', '8', '33', '12', '7', '4', '72', '2', '42', '13', '']

So when you try to cast the last item in the list ( '' ) to an int , you get ValueError: invalid literal for int() with base 10: '' .因此,当您尝试将列表中的最后一项 ( '' ) 转换为int时,您会得到ValueError: invalid literal for int() with base 10: ''

Bonus: file1.read().splitlines()奖励:file1.read().splitlines()

Another option is the splitlines method which will split on newlines but strip any leading or trailing whitespace.另一种选择是splitlines方法,它将在换行符处拆分,但删除任何前导或尾随空格。

with open("file1.txt", "r") as file1:
    file1.read().splitlines()

# ['3', '6', '5', '8', '33', '12', '7', '4', '72', '2', '42', '13']

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM