简体   繁体   English

Python读取文本文件

[英]Python Reading a text file

I wrote an applescript to generate a text file of all unwatched movies in my itunes library. 我编写了一个applescript,以生成iTunes库中所有未观看电影的文本文件。 Here is the code of that script: 这是该脚本的代码:

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

Here is my output file: 这是我的输出文件:

/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

My problem is reading this file. 我的问题是读取此文件。 I need to open it, and put it in a string so that I can search it wholeistically (ie, if I search for "Think Like a Man" I don't want a false positive on "Think Like a Man Too". 我需要打开它,并将其放在字符串中,以便可以进行整体搜索(即,如果我搜索“像男人一样思考”,我不希望对“像男人一样思考”误判。

Anyway, here is my current python code (or at least a snippet of that code): 无论如何,这是我当前的python代码(或至少该代码的一小段):

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

Here is the output: 这是输出:

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

Clearly, it's opening and reading the file, but I'm not understanding why the snippet isn't providing a full "list" of what's in the text file. 显然,它正在打开并读取文件,但我不明白为什么该代码片段没有提供文本文件中内容的完整“列表”。 Any thoughts as to what I'm doing wrong? 关于我在做什么错有什么想法吗?

Here is the final answer. 这是最终答案。 Not sure exactly what's different than what I had before (mixing tabs and spaces maybe??). 不确定与我之前有什么不同(可能混用制表符和空格?)。 Anyway, for future reference, this code: 无论如何,此代码供以后参考:

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

Gave the output of this list: 给出了该列表的输出:

['/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']

which was exactly what I needed. 这正是我所需要的。 I list with no "\\r" or "\\n"'s. 我没有列出“ \\ r”或“ \\ n”。 Thanks for the help! 谢谢您的帮助!

Is this your actual code snippet? 这是您的实际代码段吗?

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

This would only print the last line of your file. 这只会打印文件的最后一行。 Note, print line is only indented one tab . 注意, print line仅缩进一个tab

Try calling .read() on the file you open and then split the content up on \\n (newline) 尝试在打开的文件上调用.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

But this is probably enough for your usecase: 但这对于您的用例可能就足够了:

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

Try This ( Pythonic Way , UPDATED ) : 试试这个( Pythonic方式 ,更新):

import sys

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

Python 2x 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