file-io

How to read file N lines at a time in Python?

偶尔善良 提交于 2019-12-27 12:21:32
问题 I need to read a big file by reading at most N lines at a time, until EOF. What is the most effective way of doing it in Python? Something like: with open(filename, 'r') as infile: while not EOF: lines = [get next N lines] process(lines) 回答1: One solution would be a list comprehension and the slice operator: with open(filename, 'r') as infile: lines = [line for line in infile][:N] After this lines is tuple of lines. However, this would load the complete file into memory. If you don't want

How to read file N lines at a time in Python?

戏子无情 提交于 2019-12-27 12:21:30
问题 I need to read a big file by reading at most N lines at a time, until EOF. What is the most effective way of doing it in Python? Something like: with open(filename, 'r') as infile: while not EOF: lines = [get next N lines] process(lines) 回答1: One solution would be a list comprehension and the slice operator: with open(filename, 'r') as infile: lines = [line for line in infile][:N] After this lines is tuple of lines. However, this would load the complete file into memory. If you don't want

Import CSV file with mixed data types

二次信任 提交于 2019-12-27 12:20:04
问题 I'm working with MATLAB for few days and I'm having difficulties to import a CSV-file to a matrix. My problem is that my CSV-file contains almost only Strings and some integer values, so that csvread() doesn't work. csvread() only gets along with integer values. How can I store my strings in some kind of a 2-dimensional array to have free access to each element? Here's a sample CSV for my needs: 04;abc;def;ghj;klm;;;;; ;;;;;Test;text;0xFF;; ;;;;;asdfhsdf;dsafdsag;0x0F0F;; The main thing are

Import CSV file with mixed data types

删除回忆录丶 提交于 2019-12-27 12:18:07
问题 I'm working with MATLAB for few days and I'm having difficulties to import a CSV-file to a matrix. My problem is that my CSV-file contains almost only Strings and some integer values, so that csvread() doesn't work. csvread() only gets along with integer values. How can I store my strings in some kind of a 2-dimensional array to have free access to each element? Here's a sample CSV for my needs: 04;abc;def;ghj;klm;;;;; ;;;;;Test;text;0xFF;; ;;;;;asdfhsdf;dsafdsag;0x0F0F;; The main thing are

Python read JSON file and modify

随声附和 提交于 2019-12-27 12:17:11
问题 Hi I am trying to take the data from a json file and insert and id then perform POST REST. my file data.json has: { 'name':'myname' } and I would like to add an id so that the json data looks like: { 'id': 134, 'name': 'myname' } So I tried: import json f = open("data.json","r") data = f.read() jsonObj = json.loads(data) I can't get to load the json format file. What should I do so that I can convert the json file into json object and add another id value. 回答1: Set item using data['id'] = ...

How do I read in the contents of a directory in Perl?

社会主义新天地 提交于 2019-12-27 12:04:19
问题 How do I get Perl to read the contents of a given directory into an array? Backticks can do it, but is there some method using 'scandir' or a similar term? 回答1: opendir(D, "/path/to/directory") || die "Can't open directory: $!\n"; while (my $f = readdir(D)) { print "\$f = $f\n"; } closedir(D); EDIT: Oh, sorry, missed the "into an array" part: my $d = shift; opendir(D, "$d") || die "Can't open directory $d: $!\n"; my @list = readdir(D); closedir(D); foreach my $f (@list) { print "\$f = $f\n";

Getting the inputstream from a classpath resource (XML file)

一世执手 提交于 2019-12-27 11:27:06
问题 In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it? 回答1: ClassLoader.getResourceAsStream(). As stated in the comment below, if you are in a multi- ClassLoader environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader() . See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388

Getting the inputstream from a classpath resource (XML file)

落爺英雄遲暮 提交于 2019-12-27 11:25:06
问题 In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it? 回答1: ClassLoader.getResourceAsStream(). As stated in the comment below, if you are in a multi- ClassLoader environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader() . See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388

Elegant ways to count the frequency of words in a file

我与影子孤独终老i 提交于 2019-12-27 11:04:56
问题 What are the elegant and effective ways to count the frequency of each "english" word in a file? 回答1: First of all, I define letter_only std::locale so as to ignore punctuations coming from the stream, and to read only valid "english" letters from the input stream. That way, the stream will treat the words "ways" , "ways." and "ways!" as just the same word "ways" , because the stream will ignore punctuations like "." and "!" . struct letter_only: std::ctype<char> { letter_only(): std::ctype

Elegant ways to count the frequency of words in a file

我的未来我决定 提交于 2019-12-27 11:04:38
问题 What are the elegant and effective ways to count the frequency of each "english" word in a file? 回答1: First of all, I define letter_only std::locale so as to ignore punctuations coming from the stream, and to read only valid "english" letters from the input stream. That way, the stream will treat the words "ways" , "ways." and "ways!" as just the same word "ways" , because the stream will ignore punctuations like "." and "!" . struct letter_only: std::ctype<char> { letter_only(): std::ctype