改变阅读文件的方向?

时间:2015-09-25 21:47:25

标签: python file python-2.7

我有一个adafruit LCD屏幕,希望按钮能够向下和向上导航到文本文件中的文本。

我花了一整天时间来解决这个问题。

到目前为止,我有这个:

if lcd.is_pressed(LCD.UP):
    lcd.clear()
    text = txt.readline()
    lcd.message(("%s") % text)
    print "%s" % text
    time.sleep (0.5)
elif lcd.is_pressed(LCD.DOWN):
    lcd.clear()
    # this is where I need help can you do read line last or somthing
    text = txt.readline() 
    lcd.message(("%s") % text)
    print "%s" % text
    time.sleep (0.5)

我有所有的位来驱动液晶显示器工作,但只能按下文件。

3 个答案:

答案 0 :(得分:2)

您可以将整个文件作为行数组加载到内存中,并使用指针(整数,最初为0)。每按一次按钮,递增/递减指针并显示相应的行。

如果文件太大而无法一次加载到内存中,您只需记录最后的 N 文件指针位置(使用private BankAccount[] bank = new BankAccount[5]; )。按下后退按钮时,将指针重置为所需位置(使用txt.tell())并读取该行。按下前进按钮时,正常读取线并记录指针位置。

答案 1 :(得分:2)

Python提供了linecache module,虽然内置了支持友好的回溯,但对大多数文本文件都可以正常工作。它分割了将所有行读入内存并实时读取文件之间的区别,它的设计基本上适用于您正在考虑的场景(您在相邻行之间移动,帮助缓存)。

import linecache

...

lineno = 0
txtfilename = '...'

...

if lcd.is_pressed(LCD.UP) or lcd.is_pressed(LCD.DOWN):
    lineno += 1 if lcd.is_pressed(LCD.UP) else -1  # These numbers match the behavior in your example, but I think you may want to reverse them, so UP goes up one line...
    lcd.clear()
    text = linecache.getline(txtfilename, lineno)
    lcd.message(("%s") % text)
    print "%s" % text
    time.sleep (0.5)

答案 2 :(得分:0)

如果您的文本文件太大而不适合内存,最简单的方法是将整个文件读入列表(我假设library(rvest) findHome <- function(artist) { ##function to look for the table with the right info tableMusic <- function(data) { if(!any(grepl("years active|labels|instruments", data[,1], ignore.case=T))) { for (i in 2:5) { data <- try(url %>% html %>% html_nodes(xpath=paste('//*[@id="mw-content-text"]/table[', i, ']', sep="")) %>% html_table(fill=T), silent=T) if(!class(data)=="try-error" & length(data)>0) { if(class(data)!="data.frame") {data <- data.frame(data, stringsAsFactors=F)} if(any(grepl("years active|labels|instruments", data[,1], ignore.case=T))) { break } } } } if(class(data)=="try-error" | length(data)<1) { data <- NULL } else if (!any(grepl("years active|labels|instruments", data[,1], ignore.case=T))) { data <- NULL } data } #function to pull data and try different pages if the first is wrong artistData <- function(artist) { artist <- gsub(" ", "_", artist) artist <- gsub("'", "%27", artist) ##first try getting the data url <- paste("https://en.wikipedia.org/wiki/", artist, sep="") data <- try(url %>% html %>% html_nodes(xpath='//*[@id="mw-content-text"]/table[1]') %>% html_table(fill=T), silent=T) ##check if it's the right page (deal with disambiguation issues) if(!class(data)=="try-error" & length(data)>0) { if(class(data)!="data.frame") {data <- data.frame(data, stringsAsFactors=F)} data <- tableMusic(data) } ## if try-error or musicTable==NULL, try _(band) if(class(data)=="try-error" | is.null(data) | length(data)<1) { url <- paste("https://en.wikipedia.org/wiki/", artist, "_(band)", sep="") data <- try(url %>% html %>% html_nodes(xpath='//*[@id="mw-content-text"]/table[1]') %>% html_table(fill=T), silent=T) if(class(data)=="try-error"){ data <- NULL } else { if(class(data)!="data.frame") {data <- data.frame(data, stringsAsFactors=F)} data <- tableMusic(data) } } else { if(class(data)!="data.frame") {data <- data.frame(data, stringsAsFactors=F)} } ## if try-error or musicTable==NULL, try _(musician) if(class(data)=="try-error" | is.null(data) | length(data)<1) { url <- paste("https://en.wikipedia.org/wiki/", artist, "_(musician)", sep="") data <- try(url %>% html %>% html_nodes(xpath='//*[@id="mw-content-text"]/table[1]') %>% html_table(fill=T), silent=T) if(class(data)=="try-error"){ data <- NULL } else { if(class(data)!="data.frame") {data <- data.frame(data, stringsAsFactors=F)} data <- tableMusic(data) } } else { if(class(data)!="data.frame") {data <- data.frame(data, stringsAsFactors=F)} } data } ## first try finding data data <- artistData(artist) ## try finding with and/& if(is.null(data)){data <- artistData(unlist(strsplit(artist, " and| &"))[1])} ## if no matches return "" if(class(data)=="try-error" | is.null(data)) { data <- "" return() } else { if(class(data)!="data.frame") {data <- data.frame(data, stringsAsFactors=F)} } ## if we have a matching page, pull the relevant data origin <- data[data[,1]=="Origin",2] if(length(origin)>0) { home <- origin } else { born <- data[data[,1]=="Born",2] if (length(born)>0) { home <- unlist(strsplit(born, "age.[0-9]+)"))[2] } else { home <- "" } } home } findHome("randy newman") 是您的打开文件参考):

txt

现在text = txt.readlines() 将是您文件的第一行,text[0]是第二行,等等。您可以设置一个变量,让它称之为text[1],为0然后根据按下的按钮增加或减少该值,并每次使用

将正确的线路发送到LCD
line