简体   繁体   English

从文件读取行

[英]Reading lines from a file

I am writing a program to read lines of text from 5 files and compile the text from these 5 files into respective lists. 我正在编写一个程序,以从5个文件中读取文本行,并将这5个文件中的文本编译为相应的列表。

However I am having a great deal of trouble getting the program to actually read the text to a list, here is my code so far: 但是,要让程序实际将文本读取到列表中会遇到很多麻烦,这是到目前为止的代码:

from random import random

from dice import choose

b = open ('E:\Videos, TV etc\Python\ca2\beginning.txt', 'r'). readlines ()
stripped_b = [item.strip() for item in b]

a = open ('E:\Videos, TV etc\Python\ca2\adjective.txt', 'r'). readlines ()
stripped_a = [item.strip() for item in a]

i = open ('E:\Videos, TV etc\Python\ca2\inflate.txt', 'r'). readlines ()
stripped_i = [item.strip() for item in i]

n = open ('E:\Videos, TV etc\Python\ca2\noun.txt', 'r'). readlines ()
stripped_n = [item.strip() for item in n]

phrase = []

turn = 0

def business_phrase(x):

    x = raw_input("\n\nInsert a number of business phrases to generate: ")
    while turn <= x:
        turn += 1
        for item in stripped_b:
            random_word = choose(item)
            phrase.append(random_word)
        for item in stripped_a:
            random_word = choose(item)
            phrase.append(random_word)
        for item in stripped_i:
            random_word = choose(item)
            phrase.append(random_word)
        for item in stripped_n:
            random_word = choose(item)
            phrase.append(random_word)
    print random_list

business_phrase(x) business_phrase(x)

where beginning, adjective, inflate and noun are the text files and dice is a python file containing the function for choose. 其中,开头,形容词,膨胀和名词是文本文件,而dice是包含选择功能的python文件。

I run this program to try and generate a phrase and I get the following error message: 我运行该程序尝试生成一个短语,然后收到以下错误消息:

IOError: [Errno 22] invalid mode ('r') or filename 'E:\\Videos, Tv etc\\Python\\ca2\\x08eginning.txt'

I have no idea why it won't read the text files as they are in the same directory as stated (in fact in the same directory as the program containing the function). 我不知道为什么它不读取文本文件,因为它们位于与所述相同的目录中(实际上与包含该函数的程序位于同一目录中)。

Does anyone have any ideas I am completely stumped. 有谁有任何想法让我完全迷住了。

when handling Windows pathnames, always use raw string literals: 处理Windows路径名时,请始终使用原始字符串文字:

r'E:\Videos, TV etc\Python\ca2\beginning.txt'

because otherwise the backslashes may be interpreted as starting an escape sequence. 因为否则反斜杠可以解释为开始转义序列。

Also, watch out with backslashes at the end of a string: r'C:\\' is not what you think it is. 另外,请注意在字符串末尾加反斜杠: r'C:\\'不是您认为的那样。

'\\' is used for escapes in Python strings. '\\'用于Python字符串中的转义。 Here's a list of what you can do with them . 这是您可以使用它们执行的操作的列表 You want to escape the backslashes to actually have them read as backslahes, which means using two of them. 您想转义反斜杠以使其实际上读作反斜杠,这意味着要使用其中的两个。 Do this: 做这个:

'E:\\Videos, TV etc\\Python\\ca2\\adjective.txt'

Raw strings like larsmans suggested will also work! 建议使用像larsmans这样的原始字符串也可以!

No, you probably don't have a filename with a ^H in it. 不,您可能没有一个带有^ H的文件名。 Escape your backslashes. 转义您的反斜杠。

'...\\...'

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

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