标签: file-io

在Python中对字符串进行高效的可变字节迭代

我正在用Python读取一个大的(500MB)二进制文件,并将其逐字节解析为Python数据结构.此文件表示稀疏数据网格.有时我需要读取一个字节,两个字节或四个字节,具体取决于格式.出于官僚主义的原因,我需要在Python而不是C中执行此操作.

我正在寻找运行时有效的机制来在Python中执行此操作.下面是我现在正在做的一个简化示例:

with open(filename,'rb') as inFile:
 nCoords = struct.unpack('!i',inFile.read(4))[0]
 for i in range(nCoords):
    coord = (struct.unpack_from('!h',inFile.read(2))[0],struct.unpack_from('!h',inFile.read(2))[0]) # x, y coord
    nCrops = struct.unpack_from('!B',inFile.read(1))[0] #n crops
    for j in range(nCrops):
        cropId = struct.unpack_from('!B',inFile.read(1))[0] #cropId
Run Code Online (Sandbox Code Playgroud)

我想知道是否将文件从磁盘加载到字符串中,解析出字符串比一次读取几个字节更有效.就像是:

with open(filename,'rb') as inFile:
   wholeFile = inFile.read()
Run Code Online (Sandbox Code Playgroud)

但我怀疑使用数组拼接wholeFile会比我现在做的更有效.

在Python中是否有一个运行时有效的机制将文件读入字符串,然后一次迭代几个字节? (我已经检查过StringIO,它只允许一次读一行,而不是我想要的,因为整个文件是一行).

python file-io

0
推荐指数
1
解决办法
1236
查看次数

无法打开文件

我正在创建一个程序来读取FASTA文件并拆分某些特定字符,例如' >'等等.但是我遇到了问题.

计划部分是:

>>> def read_FASTA_strings(seq_fasta):
...     with open(seq_fasta.txt) as file: 
...             return file.read().split('>') 
Run Code Online (Sandbox Code Playgroud)

错误---

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'seq_fasta' is not defined
Run Code Online (Sandbox Code Playgroud)

如何摆脱这个问题?

python file-io

0
推荐指数
1
解决办法
633
查看次数

如何将文件下载为浏览器

如果我通过浏览器保存mp3文件,第三方库通常与他合作,如果我通过HTTP摇摆自己,那么第三方库由于没有正确编码而无法工作.

我用这个代码

HttpGet first = new HttpGet(url);

first.addHeader("Content-Type", "audio/mpeg");

HttpResponse response = httpclient.execute(first, localContext);
InputStream instream = response.getEntity().getContent();

StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(instream));
for (String line = r.readLine(); line != null; line = r.readLine()) {
    sb.append(line);
}

instream.close();
String textFile = sb.toString();

BufferedWriter out = new BufferedWriter(new FileWriter("test123.mp3"));
out.write(textFile);
out.close();
Run Code Online (Sandbox Code Playgroud)

怎么了?

也许它编码

java file-io http

0
推荐指数
1
解决办法
299
查看次数

如何使用正则表达式读取文本文件中的类,ID或标签样式?

我有一个正则表达式的问题,我需要一个正则表达式来读取具有属性的类名称和ID和标签样式相同的东西,每个匹配必须有一个块; 类名(或类名)及其风格!

要解释我的问题,正则表达式必须符合以下样式语法:

#myId {
    margin: 10px 8px 9px 0;
}

.myClass1 {
    margin: 10px -8px 9px 0;
    padding: 10px 8px 9px 0;
}

-my-tag {
    margin-down     :-10px;
    margin-left: 10px;
    margin-right: 10px;
}

-my-tag #my-id{
    margin-down     :-10px;
    margin-left: 10px;
    margin-right: 10px;
}
#_myI4D6 {margin:10px;}

.myclass.myclass_too {
}
.myclass .myclass_too {                                
    margin-up:10px;
    bac: url "(../../image/p_n-G.png)";
    Margin: 10px 0 10PX 10Px;
    }

#myID .myclass:first-child my-tag {
     margin:     10px;
}

.emptyclass {
}
.classname 
tagnam {
  padding: 40px 999px        ; 
}

.className #myid …
Run Code Online (Sandbox Code Playgroud)

.net css c# regex file-io

0
推荐指数
1
解决办法
236
查看次数

用C编写简单文件的Unix编程

我必须编写一个简单的程序,将16字节的数据输出到0和48位的文件,并提出了这个程序:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int f = create("test.tmp",774);
    if(f>0)
    {
        write(f,"DEPARTMENT OF CS",16);
        lseek(f,48,SEEK_SET);
        write(f,"DEPARTMENT OF IS",16);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这有什么问题?它告诉我什么时候使用cc 7a.sh -ll它编译:

undefined引用'create'

c unix file-io

0
推荐指数
1
解决办法
116
查看次数

RapidXML从文件中读取 - 这里有什么问题?

这两种读取输入文件的方法有什么区别?

1)使用 'ifstream.get()'

2)使用vector<char>with ifstreambuf_iterator<char> (我不太了解!)

(除了使用漂亮的矢量方法的明显答案)

输入文件是XML,如下所示,立即解析为rapidxml文档.(在其他地方初始化,参见示例main函数.)

首先,让我向您展示两种编写'load_config'函数的方法,一种使用ifstream.get(),一种使用vector<char>

方法1 ifstream.get()提供了工作代码和一个安全的rapidXML文档对象:

rapidxml::xml_document<> *load_config(rapidxml::xml_document<> *doc){
   ifstream myfile("inputfile");

   //read in config file
   char ch;
   char buffer[65536];
   size_t chars_read = 0;

   while(myfile.get(ch) && (chars_read < 65535)){
      buffer[chars_read++] = ch;
   }
   buffer[chars_read++] = '\0';

   cout<<"clearing old doc"<<endl;
   doc->clear();

   doc->parse<0>(buffer);

   //debug returns as expected here
   cout << "load_config: Name of my first node is: " << doc->first_node()->name() << "\n";

   return doc;
}
Run Code Online (Sandbox Code Playgroud)

方法2导致另一个库的cloberred rapidXML文档 - 特别是对curl_global_init(CURL_GLOBAL_SSL)的调用[参见下面的主要代码] - 但我还没有把它归咎于curl_global_init. …

c++ file-io rapidxml code-formatting libcurl

0
推荐指数
1
解决办法
3840
查看次数

简单的C++文件I/O问题

自从我在C++中使用File I/O(一般只是C++)以来已经有一段时间了,但我最近决定用它来为朋友制作一个小型控制台项目.我的问题是我遇到了一些字符串数组和文件I/O的问题(我不确定是哪个引起了问题).我的代码如下(ReadPSWDS是一个ifstream):

                int i = 0;
            string str[200];

            ReadPSWDS.clear();
            ReadPSWDS.open("myPasswords.DoNotOpen");

            if(ReadPSWDS.is_open())
            {
                while(!ReadPSWDS.eof())
                {
                    getline(ReadPSWDS, str[i]); //Store the line
                    if(str[i].length()<1 || str[i] == "")
                    {
                        //Ignore the line if it's nothing
                    }
                    else
                    {
                        i++; //Move onto the next 'cell' in the array
                    }
                }
            }

            ReadPSWDS.close();
Run Code Online (Sandbox Code Playgroud)

我的问题是,在测试时,字符串数组似乎是空的(并且在将所有这些行写入文件时,文件按预期为空).为什么字符串数组为空并且未填充文本文件的相应行?

问候,

c++ file-io

0
推荐指数
1
解决办法
195
查看次数

我怎样才能在java中使用unsigned

我需要转换integerbyte并在文件中写字节,但是当我转换大于128的数字节转换为负数.我需要使用unsigned char,但我不知道如何.在c ++我们写,unsigned但它在java中是怎么回事?

java file-io

0
推荐指数
1
解决办法
208
查看次数

在Python中跳过二进制文件中的数据

我正在为我的(二进制)格式编写文件阅读器,有时我需要跳过文件的某些部分.

我可以做到fileobject.read(howmuchtoskip)这一点,但我想这会将不需要的部分加载到内存中并且会很慢.

我可以设置"指针"的"索引"或者所谓的"指针"吗?

python file-io

0
推荐指数
1
解决办法
552
查看次数

从.txt文件初始化对象向量

#include<iostream>
#include<vector>
#include<fstream>
#include "stock.h"
int main(){
    double balance =0, tempPrice=0;
    string tempStr;

    vector < Stock > portfolio;
    typedef vector<Stock>::iterator StockIt;

    ifstream fileIn( "Results.txt" );
    for(StockIt i = portfolio.begin(); i != portfolio.end(); i++)
    {

        while ( !fileIn.eof( ))
        {
            getline(fileIn,tempStr);
            i->setSymbol(tempStr);

            fileIn >> tempPrice;
            i->setPrice(tempPrice);

            getline(fileIn,tempStr);
            i->setDate(tempStr);
        }
        fileIn.close();
    }
    for(StockIt i = portfolio.begin(); i != portfolio.end(); i++){
        cout<<i->getSymbol() <<endl;
        cout<<i->getPrice() <<endl;
        cout<<i->getDate() <<endl;
    }
    return 0;
Run Code Online (Sandbox Code Playgroud)

}

示例文本文件Results.txt:

GOOG    569.964 11/17/2010
MSFT    29.62   11/17/2010
YHOO    15.38   11/17/2010
AAPL    199.92 …
Run Code Online (Sandbox Code Playgroud)

c++ file-io object data-structures

0
推荐指数
1
解决办法
1502
查看次数

标签 统计

file-io ×10

c++ ×3

python ×3

java ×2

.net ×1

c ×1

c# ×1

code-formatting ×1

css ×1

data-structures ×1

http ×1

libcurl ×1

object ×1

rapidxml ×1

regex ×1

unix ×1