genfromtxt

using genfromtxt to split data

限于喜欢 提交于 2019-12-11 01:26:37
问题 With Python, I am using genfromtxt (from numpy) to read in a text file into an array: y = np.genfromtxt("1400list.txt", dtype=[('mystring','S20'),('myfloat','float')]) Which works okay, except it doesn't seem to read my 2 columns into a 2D array. I am getting: [('string001', 123.0),('string002', 456.0),('string002', 789.0)] But I think would like: [['string001', 123.0],['string002', 456.0],['string002', 789.0]] I basically want each piece of information as a separate element that I can then

NumPy: using loadtxt or genfromtxt to read a ragged structure

心已入冬 提交于 2019-12-10 23:58:21
问题 I need to read an ASCII file into Python, where an excerpt of the file looks like this: E M S T N... ... 9998 1 1 128 10097 10098 10199 10198 20298 20299 20400 20399 9999 1 1 128 10098 10099 10200 10199 20299 20300 20401 20400 10000 1 1 128 10099 10100 10201 10200 20300 20301 20402 20401 10001 1 2 44 2071 2172 12373 12272 10002 1 2 44 2172 2273 12474 12373 The above should ideally be following NumPy schema: array([(9998, 1, 1, 128, (10097, 10098, 10199, 10198, 20298, 20299, 20400, 20399)),

How to control genfromtxt to read rows specified?

穿精又带淫゛_ 提交于 2019-12-10 23:24:18
问题 genfromtxt can skip header and footer lines and speicfy which columns to use. But how can I control how many lines to read? Sometimes a txt file might contain several blocks with different shape. For example, a=StringIO(''' 1,2,3 1,2,3 2,3 2,3 ''') genfromtxt(a,delimiter=',',skip_header=1) This will raise an error, ValueError: Some errors were detected ! Line #4 (got 2 columns instead of 3) Line #5 (got 2 columns instead of 3) Of couse, I can do it like this: a=StringIO(''' 1,2,3 1,2,3 2,3 2

Using multiple genfromtxt on a single file

安稳与你 提交于 2019-12-10 16:34:55
问题 I'm fairly new to Python and am currently having problems with handling my input file reads. Basically I want my code to take an input file, where the relevant info is contained in blocks of 4 lines. For my specific purpose, I only care about the info in lines 1-3 of each block. A two-block example of the input I'm dealing with, looks like: #Header line 1 #Header line 2 'Mn 1', 5130.0059, -2.765, 5.4052, 2.5, 7.8214, 1.5, 1.310, 2.390, 0.500, 8.530,-5.360,-7.630, ' LS 3d6.(5D).4p z6F*' ' LS

Importing data and variable names from a text file in Python

北战南征 提交于 2019-12-09 00:04:36
问题 I have a text file containing simulation data (60 columns, 100k rows): a b c 1 11 111 2 22 222 3 33 333 4 44 444 ... where in the first row are variable names, and beneath (in columns) is the corresponding data (float type). I need to use all these variables with their data in Python for further calculations. For example, when I insert: print(b) I need to receive the values from the second column. I know how to import data: data=np.genfromtxt("1.txt", unpack=True, skiprows = 1) Assign

Fastest way to read every n-th row with numpy's genfromtxt

泪湿孤枕 提交于 2019-12-07 17:11:01
问题 I read my data with numpy's genfromtxt: import numpy as np measurement = np.genfromtxt('measurementProfile2.txt', delimiter=None, dtype=None, skip_header=4, skip_footer=2, usecols=(3,0,2)) rows, columns = np.shape(measurement) x=np.zeros((rows, 1), dtype=measurement.dtype) x[:]=394 measurement = np.hstack((measurement, x)) np.savetxt('measurementProfileFormatted.txt',measurement) this works fine. But i want only ever 5-th , 6-th (so n-th ) row in the final Output file. According to numpy

how to load a file with date and time as a datetime object in python?

流过昼夜 提交于 2019-12-06 17:43:25
need to load this file with date in first col and HH:MM in second col. How does it work with a numpy.genfromtxt() ? Maybe pandas? My file looks like: 2017-Feb-11 00:00 m 4.87809 1.86737 5.04236 0.27627 1.5995 2017-Feb-11 00:05 m 4.86722 1.86711 5.00023 0.27616 1.5965 2017-Feb-11 00:10 m 4.85641 1.86690 4.95810 0.27604 1.5941 In [32]: df = pd.read_csv(filename, delim_whitespace=True, parse_dates=[0], header=None) In [33]: df[1] = pd.to_timedelta(df[1] + ':00') In [34]: df Out[34]: 0 1 2 3 4 5 6 7 0 2017-02-11 00:00:00 m 4.87809 1.86737 5.04236 0.27627 1.5995 1 2017-02-11 00:05:00 m 4.86722 1

Python genfromtext multiple datatypes

為{幸葍}努か 提交于 2019-12-06 12:18:56
问题 I would like to read in a csv file using genfromtxt. I have six columns that are float, and one column that is a string. How do I set the datatype so that the float columns will be read in as floats and the string column will be read in as strings? I tried dtype='void' but that is not working. Suggestions? Thanks .csv file 999.9, abc, 34, 78, 12.3 1.3, ghf, 12, 8.4, 23.7 101.7, evf, 89, 2.4, 11.3 x = sys.argv[1] f = open(x, 'r') y = np.genfromtxt(f, delimiter = ',', dtype=[('f0', '<f8'), ('f1

NumPy genfromtxt: using filling_missing correctly

回眸只為那壹抹淺笑 提交于 2019-12-05 03:46:57
I am attempting to process data saved to CSV that may have missing values in an unknown number of columns (up to around 30). I am attempting to set those missing values to '0' using genfromtxt 's filling_missing argument. Here is a minimal working example for numpy 1.6.2 running in ActiveState ActivePython 2.7 32 bit on Win 7. import numpy text = "a,b,c,d\n1,2,3,4\n5,,7,8" a = numpy.genfromtxt('test.txt',delimiter=',',names=True) b = open('test.txt','w') b.write(text) b.close() a = numpy.genfromtxt('test.txt',delimiter=',',names=True) print "plain",a a = numpy.genfromtxt('test.txt',delimiter='

Python: Convert string (in scientific notation) to float

六眼飞鱼酱① 提交于 2019-12-03 19:20:54
问题 I'm trying to import a large .csv file containing text and numbers using genfromtxt in numpy. I'm only interested in two columns. I have most of the import sorted out with: def importfile(root): data = root.entry.get() atw = np.genfromtxt(data, delimiter=",", skip_header=1, skip_footer=2, autostrip=True, usecols=(25,26), dtype=("|S10")) elem = atw[:,0] concs = atw[:,1] print(elem) print(concs) With output for elem and concs respectively: ['Na2O' 'MgO' 'Al2O3' 'SiO2' 'P2O5' 'SO3' 'Cl' 'K2O'