小编use*_*551的帖子

使用python从重定向的stdin读取文件

我试图通过命令行读取重定向stdin的文本文件的内容,并在接收器必须将其组装回原始形式时通过Internet发送它.

例如:

$ python test.py < file.txt
Run Code Online (Sandbox Code Playgroud)

我试图读取该文件,并把它与灵感来自于下面的代码汇编回链接:

for line in sys.stdin:
  stripped = line.strip()
  if not stripped: break
  result = result + stripped

print "File is beeing copied"
file = open("testResult.txt", "w")
file.write(result)
file.close()
print "File copying is complete!"
Run Code Online (Sandbox Code Playgroud)

但是这个解决方案可以工作,只要我没有空行(两个'\n'一个接一个)在我的文件中,如果我有,我的循环中断和文件读取结束.我怎样才能从stdin读取我到达重定向文件的<>?

python shell stdin

5
推荐指数
1
解决办法
6191
查看次数

读取文件中的第一行在第一行中给出了"\ 357\273\277"前缀

当我使用函数readTheNRow with row = 0(我读第一行)时,我发现三个第一个字符是\ 357,\ 273和\ 277.我发现这个前缀是与UTF-8文件有关的一些,但有些文件有这个前缀,有些文件没有:(.我如何忽略我想从中读取的文件中所有类型的此类前缀?

int readTheNRow(char buff[], int row) {

int file = open("my_file.txt", O_RDONLY);
if (file < 0) {
    write(2, "closing fifo was unsuccessful\n", 31);
    exit(-1);
}

// function's variables
int i = 0;
char ch; // a temp variable to read with it
int check; // helping variable for checking the read function

// read till we reach the needed row
while (i != row) {

    // read one char
    check = read(file, &ch, …
Run Code Online (Sandbox Code Playgroud)

c file-io byte-order-mark

3
推荐指数
1
解决办法
6726
查看次数

标签 统计

byte-order-mark ×1

c ×1

file-io ×1

python ×1

shell ×1

stdin ×1