繁体   English   中英

Python读取文本文件

[英]Python Reading a text file

我编写了一个applescript,以生成iTunes库中所有未观看电影的文本文件。 这是该脚本的代码:

tell application "iTunes"
    set watchedEpisodes to tracks of playlist "TV Shows" whose unplayed is false and played count > 0
    set unwatchedEpisodes to tracks of playlist "TV Shows" whose unplayed is true
    if (count unwatchedEpisodes) > 0 then
        set trackNames to {}
        repeat with anEpisode in unwatchedEpisodes
            set tmp to location of anEpisode as text
            set posixtmp to POSIX path of tmp
            set end of trackNames to (posixtmp as text)
        end repeat

        set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}

        set unwatchedTVShows to trackNames as text
        do shell script "echo " & quoted form of unwatchedTVShows & " > /Volumes/Data/Media/Kodi/unwatchedTVShows.txt"
        #display dialog trackNames as text
        set AppleScript's text item delimiters to TID
    end if

    set watchedEpisodes to tracks of playlist "Movies" whose unplayed is false and played count > 0
    set unwatchedEpisodes to tracks of playlist "Movies" whose unplayed is true
    if (count unwatchedEpisodes) > 0 then
        set trackNames to {}
        repeat with anEpisode in unwatchedEpisodes
            set tmp to location of anEpisode as text
            set posixtmp to POSIX path of tmp
            set end of trackNames to (posixtmp as text)

        end repeat

        set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}

        set unwatchedMovies to trackNames as text
        do shell script "echo " & quoted form of unwatchedMovies & " > /Volumes/Data/Media/Kodi/unwatchedMovies.txt"
        #display dialog trackNames as text
        set AppleScript's text item delimiters to TID
    end if
end tell

这是我的输出文件:

/Volumes/Data/Media/Movies/Thanks For Sharing.m4v
/Volumes/Data/Media/Movies/Thats My Boy.m4v
/Volumes/Data/Media/Movies/Think Like a Man.m4v
/Volumes/Data/Media/Movies/Think Like a Man Too.m4v
/Volumes/Data/Media/Movies/This Means War.m4v
/Volumes/Data/Media/Movies/Top Five.m4v
/Volumes/Data/Media/Movies/Trail of Blood.m4v

我的问题是读取此文件。 我需要打开它,并将其放在字符串中,以便可以进行整体搜索(即,如果我搜索“像男人一样思考”,我不希望对“像男人一样思考”误判。

无论如何,这是我当前的python代码(或至少该代码的一小段):

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    for line in umovies:
        line = line.strip()
        print line

这是输出:

/Volumes/Data/Media/Movies/Trail of Blood.m4voo.m4v

显然,它正在打开并读取文件,但我不明白为什么该代码片段没有提供文本文件中内容的完整“列表”。 关于我在做什么错有什么想法吗?

这是最终答案。 不确定与我之前有什么不同(可能混用制表符和空格?)。 无论如何,此代码供以后参考:

with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as uwmovies:
    for line in uwmovies:
        line = line.replace("\n","")
        unwatchedmovies = line.split('\r')
        print unwatchedmovies

给出了该列表的输出:

['/Volumes/Data/Media/Movies/Table 19.m4v', '/Volumes/Data/Media/Movies/Term Life.m4v', '/Volumes/Data/Media/Movies/Thanks For Sharing.m4v', '/Volumes/Data/Media/Movies/Thats My Boy.m4v', '/Volumes/Data/Media/Movies/Think Like a Man.m4v', '/Volumes/Data/Media/Movies/Think Like a Man Too.m4v', '/Volumes/Data/Media/Movies/This Means War.m4v', '/Volumes/Data/Media/Movies/Top Five.m4v', '/Volumes/Data/Media/Movies/Trail of Blood.m4v']

这正是我所需要的。 我没有列出“ \\ r”或“ \\ n”。 谢谢您的帮助!

这是您的实际代码段吗?

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    for line in umovies:
        line = line.strip()
    print line

这只会打印文件的最后一行。 注意, print line仅缩进一个tab

尝试在打开的文件上调用.read() ,然后在\\n (换行符)上拆分内容

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    for line in umovies.read().split('\n'):
        line = line.strip()
        print line

但这对于您的用例可能就足够了:

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    umovies_content = umovies.read()
    print umovies_content 

试试这个( Pythonic方式 ,更新):

import sys

read_lines = [sys.stdout.write(line.rstrip('\n') + "\n") for line in open('1.txt')]

Python 2x

from __future__ import print_function
read_lines = [print(line.rstrip('\n')) for line in open('1.txt')]

暂无
暂无

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

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