file-io

Reading utf-8 characters from a gzip file in python

假如想象 提交于 2019-12-29 04:11:32
问题 I am trying to read a gunzipped file (.gz) in python and am having some trouble. I used the gzip module to read it but the file is encoded as a utf-8 text file so eventually it reads an invalid character and crashes. Does anyone know how to read gzip files encoded as utf-8 files? I know that there's a codecs module that can help but I can't understand how to use it. Thanks! import string import gzip import codecs f = gzip.open('file.gz','r') engines = {} line = f.readline() while line: parsed

Reading utf-8 characters from a gzip file in python

余生长醉 提交于 2019-12-29 04:11:24
问题 I am trying to read a gunzipped file (.gz) in python and am having some trouble. I used the gzip module to read it but the file is encoded as a utf-8 text file so eventually it reads an invalid character and crashes. Does anyone know how to read gzip files encoded as utf-8 files? I know that there's a codecs module that can help but I can't understand how to use it. Thanks! import string import gzip import codecs f = gzip.open('file.gz','r') engines = {} line = f.readline() while line: parsed

Reading utf-8 characters from a gzip file in python

让人想犯罪 __ 提交于 2019-12-29 04:11:05
问题 I am trying to read a gunzipped file (.gz) in python and am having some trouble. I used the gzip module to read it but the file is encoded as a utf-8 text file so eventually it reads an invalid character and crashes. Does anyone know how to read gzip files encoded as utf-8 files? I know that there's a codecs module that can help but I can't understand how to use it. Thanks! import string import gzip import codecs f = gzip.open('file.gz','r') engines = {} line = f.readline() while line: parsed

Reading Huge File in Python

…衆ロ難τιáo~ 提交于 2019-12-29 03:29:47
问题 I have a 384MB text file with 50 million lines. Each line contains 2 space-separated integers: a key and a value. The file is sorted by key. I need an efficient way of looking up the values of a list of about 200 keys in Python. My current approach is included below. It takes 30 seconds. There must be more efficient Python foo to get this down to a reasonable efficiency of a couple of seconds at most. # list contains a sorted list of the keys we need to lookup # there is a sentinel at the end

Bash read/write file descriptors — seek to start of file

时光怂恿深爱的人放手 提交于 2019-12-29 03:29:27
问题 I tried to use the read/write file descriptor in bash so that I could delete the file that the file descriptor referred to afterward, as such: F=$(mktemp) exec 3<> "$F" rm -f "$F" echo "Hello world" >&3 cat <&3 but the cat command gives no output. I can achieve what I want if I use separate file descriptors for reading and writing: F=$(mktemp) exec 3> "$F" exec 4< "$F" rm -f "$F" echo "Hello world" >&3 cat <&4 which prints Hello world . I suspected that bash doesn't automatically seek to the

Read Remote File with Access Permissions

佐手、 提交于 2019-12-29 01:29:13
问题 I am trying to read a file on a network server (from a Windows XP machine), which I would normally access by asking for \\ServerName\dirPath\ in the run dialog. Right now, I have to write a program that reads the file off the server and returns some results on my PC. The problem that I'm facing is that the login name on my PC does not have a login account on the server. As a result, I am unable to read the file on the server when I tried: f = open(r'\\server\path\to\file', 'r') I know that I

How to read a float from binary file in C?

只愿长相守 提交于 2019-12-28 18:08:25
问题 Everything I'm finding via google is garbage... Note that I want the answer in C , however if you supplement your answer with a C++ solution as well then you get bonus points! I just want to be able to read some floats into an array from a binary file EDIT: Yes I know about Endian-ness... and no I don't care how it was stored. 回答1: How you have to read the floats from the file completely depends on how the values were saved there in the first place. One common way could be: void writefloat

Reading the last n lines from a huge text file

冷暖自知 提交于 2019-12-28 18:07:07
问题 I've tried something like this file_in <- file("myfile.log","r") x <- readLines(file_in, n=-100) but I'm still waiting... Any help would be greatly appreciated 回答1: I'd use scan for this, in case you know how many lines the log has : scan("foo.txt",sep="\n",what="char(0)",skip=100) If you have no clue how many you need to skip, you have no choice but to move towards either reading in everything and taking the last n lines (in case that's feasible), using scan("foo.txt",sep="\n",what=list(NULL

Can Fortran read bytes directly from a binary file?

本秂侑毒 提交于 2019-12-28 15:54:34
问题 I have a binary file that I would like to read with Fortran. The problem is that it was not written by Fortran, so it doesn't have the record length indicators. So the usual unformatted Fortran read won't work. I had a thought that I could be sneaky and read the file as a formatted file, byte-by-byte (or 4 bytes by 4 bytes, really) into a character array and then convert the contents of the characters into integers and floats via the transfer function or the dreaded equivalence statement. But

Can Fortran read bytes directly from a binary file?

為{幸葍}努か 提交于 2019-12-28 15:51:27
问题 I have a binary file that I would like to read with Fortran. The problem is that it was not written by Fortran, so it doesn't have the record length indicators. So the usual unformatted Fortran read won't work. I had a thought that I could be sneaky and read the file as a formatted file, byte-by-byte (or 4 bytes by 4 bytes, really) into a character array and then convert the contents of the characters into integers and floats via the transfer function or the dreaded equivalence statement. But