我的应用程序有一个命令行界面,我正在考虑使用GNU Readline库来提供历史记录,可编辑的命令行等.
问题是我的命令可能很长而且复杂(想想SQL),我想让用户在多行上传播命令,使它们在历史记录中更具可读性.
是否可以在readline中执行此操作(可能通过指定换行符和命令结尾之间的差异)?
或者我会更好地实现自己的命令行(但可能使用GNU历史库)?
在Python中,您可以使用读取文件并将其行加载到列表中
f = open('file.txt','r')
lines = f.readlines()
Run Code Online (Sandbox Code Playgroud)
每个单独的行由分隔,\n但如果行的内容有\r,则不将其视为新行.我需要将所有转换\r为\n并获得正确的列表lines.
如果我在.split('\r')里面做,lines我会在列表中找到列表.
我想到打开一个文件,替换所有\r到\n,关闭文件并重新阅读它,然后用readlines()但这似乎浪费.
我该如何实现呢?
我无法使用我的程序读取多个角色,我似乎无法弄清楚我的程序出了什么问题,因为我对python很新.
import serial
ser = serial.Serial(
port='COM5',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
print("connected to: " + ser.portstr)
count=1
while True:
for line in ser.read():
print(str(count) + str(': ') + chr(line) )
count = count+1
ser.close()
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果
connected to: COM5
1: 1
2: 2
3: 4
4: 3
5: 1
Run Code Online (Sandbox Code Playgroud)
实际上我在期待这个
connected to: COM5
1:12431
2:12431
Run Code Online (Sandbox Code Playgroud)
像上面提到的那样能够同时读取多个字符而不是一个接一个.
我在Mac OS 10.5上使用ipython和python 2.5.1(我实际上想使用ipython for 2.6.1,但它似乎不可用?)
我通过easy_install安装了ipython.它工作但缺少gnu readline(用ctrl-R很好地搜索命令行历史记录等)
我找到了一篇博文和其他消息来源说这可以解决
sudo easy_install -f http://ipython.scipy.org/dist/ readline
但是,这会导致构建错误readline.c,尤其是未声明的函数,例如rl_compentry_func_t和rl_catch_signals.
有没有人见过这些错误?还有另一种方法可以使用readline安装ipython吗?
我已经安装了rvm和ruby 1.9.2,两者都正常工作.使用rvm版本1.10.2.我之前安装了rvm软件包readline/zlib,但现在我现在遇到了readline的大麻烦.我正在尝试在rvm中安装ruby 1.9.3,但我总是找不到readline.
我试着遵循不同的解决方案:
rvm remove 1.9.3
rvm pkg install readline
rvm install 1.9.3 --with-readline-dir=$rvm_path/usr/ \
--with-zlib-dir=$rvm_path/usr/
rvm use 1.9.3
rvmsudo gem install bundler
bundle install (all gems installed fine)
rails c / rails s
.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/irb/completion.rb:9:in `require': \
cannot load such file -- readline (LoadError)
Run Code Online (Sandbox Code Playgroud)
我安装了libreadline-dev(版本6,当前版本)(这些软件包所需的一切)
我也尝试过以下方法:
.rvm/src/ruby-1.9.3-p0/ext/readline]$ ruby extconf.rb
checking for tgetnum() in -lncurses... yes
checking for readline/readline.h... yes
checking for readline/history.h... yes
checking for readline() in -lreadline... no
checking for readline() in -ledit... no …Run Code Online (Sandbox Code Playgroud) 我在bash中执行一个简单的脚本时遇到问题.脚本是这样的:
#! /bin/sh
read -p 'press [ENTER] to continue deleting line'
sudo sed -ie '$d' /home/hpccuser/.profile
Run Code Online (Sandbox Code Playgroud)
当我用./script执行脚本时,输出如下:
press [ENTER] to continue deleting line./script: 3: read: arg count
[sudo] password for user
Run Code Online (Sandbox Code Playgroud)
我直接在终端中运行read命令(从脚本复制并粘贴到终端),它工作正常; 它等待一个ENTER被击中(就像一个暂停).
我可以将文件读取为bytes数组
但是当我将它转换为字符串时
它将utf16字节视为ascii
如何正确转换?
package main
import ("fmt"
"os"
"bufio"
)
func main(){
// read whole the file
f, err := os.Open("test.txt")
if err != nil {
fmt.Printf("error opening file: %v\n",err)
os.Exit(1)
}
r := bufio.NewReader(f)
var s,b,e = r.ReadLine()
if e==nil{
fmt.Println(b)
fmt.Println(s)
fmt.Println(string(s))
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
假
[255 254 91 0 83 0 99 0 114 0 105 0 112 0 116 0 32 0 73 0 110 0 102 0 111 0 93 0 13 0]
S …
在bash提示符下,我可以输入Ctrl-r和Ctrl-s来执行命令历史记录的增量搜索.但是,搜索区分大小写:如果我有一个命令"cat SomeFile.txt",并且我点击"Ctrl-r somefile",我什么也得不到.
有没有办法让搜索不区分大小写?
我一直在努力工作几个小时,我无法做到正确,任何帮助将不胜感激!我的问题是如何使用该函数.readline()读取文本文件的结尾?我知道这项.readlines()工作也是如此,但我正在尝试一次处理一行.
这是我到目前为止我的代码所拥有的:
a = open("SampleTxt.txt","r")
While True:
a.readline()
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我运行它时,我得到一个无限循环,一旦它不能再读取一行就不应该停止吗?
我有一个用Coffeescript编写的简单readline shell:
rl = require 'readline'
cli = rl.createInterface process.stdin, process.stdout, null
cli.setPrompt "hello> "
cli.on 'line', (line) ->
console.log line
cli.prompt()
cli.prompt()
Run Code Online (Sandbox Code Playgroud)
运行此命令会显示提示:
$ coffee cli.coffee
hello>
Run Code Online (Sandbox Code Playgroud)
我希望能够点击Ctrl-L清除屏幕.这可能吗?
我也注意到,我不能打Ctrl-L在任一节点或咖啡 REPLs无论是.
我在Ubuntu 11.04上运行.
readline ×10
python ×4
bash ×2
c ×1
coffeescript ×1
command-line ×1
gnu ×1
go ×1
ipython ×1
libreadline ×1
macos ×1
node.js ×1
pyserial ×1
python-3.x ×1
ruby-1.9.3 ×1
rvm ×1
serial-port ×1
unicode ×1
utf-16 ×1
wait ×1