file-io

How to output NLTK chunks to file?

泪湿孤枕 提交于 2019-12-31 00:03:49
问题 I have this python script where I am using nltk library to parse,tokenize,tag and chunk some lets say random text from the web. I need to format and write in a file the output of chunked1 , chunked2 , chunked3 . These have type class 'nltk.tree.Tree' More specifically I need to write only the lines that match the regular expressions chunkGram1 , chunkGram2 , chunkGram3 . How can i do that? #! /usr/bin/python2.7 import nltk import re import codecs xstring = ["An electronic library (also

FileStream.Seek vs. Buffered Reading

淺唱寂寞╮ 提交于 2019-12-30 19:59:28
问题 Motivated by this answer I was wondering what's going on under the curtain if one uses lots of FileStream.Seek(-1) . For clarity I'll repost the answer: using (var fs = File.OpenRead(filePath)) { fs.Seek(0, SeekOrigin.End); int newLines = 0; while (newLines < 3) { fs.Seek(-1, SeekOrigin.Current); newLines += fs.ReadByte() == 13 ? 1 : 0; // look for \r fs.Seek(-1, SeekOrigin.Current); } byte[] data = new byte[fs.Length - fs.Position]; fs.Read(data, 0, data.Length); } Personally I would have

How to read from a serial port in lua

耗尽温柔 提交于 2019-12-30 18:54:08
问题 I'm new to lua and I'm trying to receive data from the port, ttyACM0, I can write to the port by: wserial = io.open("/dev/ttyACM0","w") wserial:write("hellloooo") wserial:flush() I thought since I can write to it in the same way as I would write to a file that I could read it in the same way as I would read a file. But when I try to read it (using the code below) I just end up in an infinite loop. rserial=io.open("/dev/ttyACM0","r") while chaine==nil do chaine=rserial:read() rserial:flush()

How to open and read LZMA file in-memory

末鹿安然 提交于 2019-12-30 18:50:10
问题 I have a giant file, let's call it one-csv-file.xz . It is an XZ-compressed CSV file. How can I open and parse through the file without first decompressing it to disk? What if the file is, for example, 100 GB? Python cannot read all of that into memory at once, of course. Will it page or run out of memory? 回答1: You can iterate through an LZMAFile object import lzma # python 3, try lzmaffi in python 2 with open('one-csv-file.xz') as compressed: with lzma.LZMAFile(compressed) as uncompressed:

Python: How can I access an mp3 file's metadata using Python? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-30 12:37:07
问题 This question already has answers here : Accessing mp3 Meta-Data with Python (16 answers) Closed 6 years ago . Supposing I wanted to see the Artist Name? Or add BPM information? What Python tools could I go about doing this? 回答1: There's a module called Python-ID3 that does exactly this. If you're on a Debian/Ubuntu box, its package name is python-id3 and there is example code on its website: from ID3 import * try: id3info = ID3('/some/file/moxy.mp3') print id3info id3info['TITLE'] = "Green

Read and Write to a file in perl

感情迁移 提交于 2019-12-30 12:13:29
问题 this is just an example. Lets assume the above is out.txt . I want to read out.txt and write onto the same file. <Hi > <this> <is just> <an example.> Modified out.txt . I want to add tags in the beginning and end of some lines. As I will be reading the file several times I cannot keep writing it onto a different file each time. EDIT 1 I tried using "+<" but its giving an output like this : Hi this is just an example. <Hi > <this> <is just> <an example.> **out.txt** EDIT 2 Code for reference :

numpy read .csv with complex number

删除回忆录丶 提交于 2019-12-30 11:18:26
问题 stackoverflow, I have a matrix containing complex numbers (ex. -2.2982235934153075E-11+2.1179547211742553E-9i) that I need to import to a numpy array. I've been using genfromtext(file) to parse all my other, real values, but I'm getting a nan for all complex values. Any ideas? self.raw = (genfromtxt(self.loc, delimiter=',', skip_header=9, dtype=float)) [m,n] = shape(self.raw) data = zeros((m, n-3)) data[:, :] = self.raw[:, 3::] returns: data = array([nan, nan, nan, ...]) 回答1: You can do:

How can I control the formatting when saving a matrix to a file?

╄→尐↘猪︶ㄣ 提交于 2019-12-30 10:47:11
问题 I save a matrix to a file like this: save(filepath, 'mtrx', '-ascii'); Is there a way to tell MATLAB to write 0 instead of 0.0000000e+000 values? It would be nice because it would be faster and easier to see which values differ from zero. 回答1: I suggest using DLMWRITE instead of SAVE since you're dealing with ASCII files. It will give you more control over the formatting. For example, you could create an output file delimited by spaces with a field width of 10 and 6 digits after the decimal

How to fread() structs?

余生长醉 提交于 2019-12-30 10:27:08
问题 struct book { unsigned short size_of_content; unsigned short price; unsigned char *content; }; Assume I have file that contains multiple book s, each has different size_of_content , price and content . How can I read them one book at a time and identify which book it is (check price, for example)? size_t nread2; struct book *buff = malloc(sizeof(struct book)); while( (nread2 = fread(buff, sizeof(struct book), 1, infp)) > 0 ) { printf("read a struct once \n"); } This is what I have so far. I

How to fread() structs?

百般思念 提交于 2019-12-30 10:26:40
问题 struct book { unsigned short size_of_content; unsigned short price; unsigned char *content; }; Assume I have file that contains multiple book s, each has different size_of_content , price and content . How can I read them one book at a time and identify which book it is (check price, for example)? size_t nread2; struct book *buff = malloc(sizeof(struct book)); while( (nread2 = fread(buff, sizeof(struct book), 1, infp)) > 0 ) { printf("read a struct once \n"); } This is what I have so far. I