只读取 .txt 文件中的最后一个字符 [英] Only read the last character in a .txt file
问题描述
我正在使用读取txt文件并将结果发送到arduino的处理来创建程序.我可以让它发送字符串并保持更新,但是一旦我尝试只发送最后一个字符,它就行不通了……任何人都可以帮我解决这个问题吗?基本上我需要从 txt 文件中读取最后一个字符,并通过串行将其作为字符发送到 arduino、python 或处理两者都可以工作!
I am creating a program using processing that read txt file and send the result to the arduino. I can get it to send string and keep update, but as soon as I try to just send the last char it won't work... Can anyone help me with this? Basically I need to read the last char from a txt file and send it as char through serial to arduino, python or processing both will work!
*这是我的代码 [正在处理]
*Here is my code [ Processing ]
import processing.serial.*;
import java.io.*;
int mySwitch=0;
int counter=0;
String [] subtext;
Serial myPort;
void setup(){
//Create a switch that will control the frequency of text file reads.
//When mySwitch=1, the program is setup to read the text file.
//This is turned off when mySwitch = 0
mySwitch=1;
//Open the serial port for communication with the Arduino
//Make sure the COM port is correct
myPort = new Serial(this, "COM4", 9600);
myPort.bufferUntil('\n');
}
void draw() {
if (mySwitch>0){
/*The readData function can be found later in the code. This is the call to read a CSV file on the computer hard-drive. */
readData("G:/Personal/control.txt");
/*The following switch prevents continuous reading of the text file, until we are ready to read the file again. */
mySwitch=0;
}
/*Only send new data. This IF statement will allow new data to be sent to the arduino. */
if(counter<subtext.length){
/* Write the next number to the Serial port and send it to the Arduino There will be a delay of half a second before the command is sent to turn the LED off : myPort.write('0'); */
myPort.write(subtext[counter]);
delay(500);
myPort.write('0');
delay(100);
//Increment the counter so that the next number is sent to the arduino.
counter++;
} else{
//If the text file has run out of numbers, then read the text file again in 5 seconds.
delay(5000);
mySwitch=1;
}
}
/* The following function will read from a CSV or TXT file */
void readData(String myFileName){
File file=new File(myFileName);
BufferedReader br=null;
try{
br=new BufferedReader(new FileReader(file));
String text=null;
/* keep reading each line until you get to the end of the file */
while((text=br.readLine())!=null){
* Spilt each line up into bits and pieces using a comma as a separator */
subtext = splitTokens(text,",");
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
这是我正在处理的数据
[19:54:57] [服务器线程/信息]:[@] b
[19:54:57] [Server thread/INFO]: [@] b
[19:54:57] [服务器线程/信息]:[@] a
[19:54:57] [Server thread/INFO]: [@] a
[19:54:57] [服务器线程/信息]:[@] b
[19:54:57] [Server thread/INFO]: [@] b
[19:54:57] [服务器线程/信息]:[@] a
[19:54:57] [Server thread/INFO]: [@] a
[19:54:58] [服务器线程/信息]:[@] b
[19:54:58] [Server thread/INFO]: [@] b
[19:54:58] [服务器线程/信息]:[@] a
[19:54:58] [Server thread/INFO]: [@] a
[19:54:59] [服务器线程/信息]:[@] b
[19:54:59] [Server thread/INFO]: [@] b
[20:30:23] [服务器线程/INFO]:[@] a
[20:30:23] [Server thread/INFO]: [@] a
[20:30:24] [服务器线程/信息]:[@] b
[20:30:24] [Server thread/INFO]: [@] b
[21:07:34] [服务器线程/INFO]:[@] a
[21:07:34] [Server thread/INFO]: [@] a
[21:07:35] [服务器线程/信息]:[@] b
[21:07:35] [Server thread/INFO]: [@] b
- 我真正关心的唯一值是 a/b
- 此文件将始终以这种格式更新
推荐答案
从文件中读取最后一个字符:
Reading the last character from a file:
with open(filename, 'rb+') as f:
f.seek(f.tell()-1,2) # f.seek(0,2) is legal for last char in both python 2 and 3 though
print f.read()
对于 Python 3
为了使它更通用,我们想在不打开二进制模式的情况下读取文件的倒数第二个(可以是任何字符):
For Python 3
To make it more generic say we want to read second last (can be any though) char of the file without turning on the binary mode:
with open('file.txt', 'r') as f:
f.seek(0, 2)
# seek to end of file; f.seek(0, os.SEEK_END) is legal
f.seek(f.tell() - 2, 0)
# seek to the second last char of file;
# while f.seek(f.tell()-2, os.SEEK_SET) is legal,
# f.seek(-2, 0) will throw an error.
print(f.read())
seek 是一个文件对象处理注释
Seek is a file object handling comment
file.seek(offset,position)
offset
:您需要多少个字符(即 1 表示一个字符)position
:告诉你的文件读/写操作应该从哪里开始.- 0 表示文件开始
- 1 表示文件读/写光标的当前位置
- 2 表示文件结束
offset
: How many characters you need (i.e. 1 means one character)position
: Tell where your file read/write operation should start .- 0 means starting of file
- 1 means current position of file read/write cursor
- 2 means end of file
f.seek(f.tell()-1,2)
表示到文件末尾并往回遍历一个元素
f.seek(f.tell()-1,2)
means go to the end of file and traverse back one element
print f.read()
打印从seek命令获得的值
print f.read()
prints the value obtained from the seek command
这篇关于只读取 .txt 文件中的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!