Hi,
I have a file containing four columns of data separated by tabs (\t)
and I'd like to read a specific column from it (say the third). Is
there any simple way to do this in Python?
I've found quite interesting the linecache module but unfortunately
that is (to my knowledge) only working on lines, not columns.
Any suggestion?
Thanks and regards
Francesco 7 26409
On 2008-01-11, cesco <fd**********@g mail.comwrote:
Hi,
I have a file containing four columns of data separated by tabs (\t)
and I'd like to read a specific column from it (say the third). Is
there any simple way to do this in Python?
I've found quite interesting the linecache module but unfortunately
that is (to my knowledge) only working on lines, not columns.
Any suggestion?
the csv module may do what you want.
On Jan 11, 2:15 pm, cesco <fd.calabr...@g mail.comwrote:
Hi,
I have a file containing four columns of data separated by tabs (\t)
and I'd like to read a specific column from it (say the third). Is
there any simple way to do this in Python?
I've found quite interesting the linecache module but unfortunately
that is (to my knowledge) only working on lines, not columns.
Any suggestion?
Thanks and regards
Francesco
for (i, each_line) in enumerate(open( 'input_file.txt ','rb')):
try:
column_3 = each_line.split ('\t')[2].strip()
except IndexError:
print 'Not enough columns on line %i of file.' % (i+1)
continue
do_something_wi th_column_3()
cesco wrote:
I have a file containing four columns of data separated by tabs (\t)
and I'd like to read a specific column from it (say the third). Is
there any simple way to do this in Python?
use the "split" method and plain old indexing:
for line in open("file.txt" ):
columns = line.split("\t" )
print columns[2] # indexing starts at zero
also see the "csv" module, which can read all sorts of
comma/semicolon/tab-separated spreadsheet-style files.
I've found quite interesting the linecache module
the "linecache" module seems to be quite popular on comp.lang.pytho n
these days, but it's designed for a very specific purpose (displaying
Python code in tracebacks), and is a really lousy way to read text files
in the general case. please unlearn.
</F>
A.T.Hofkamp wrote:
On 2008-01-11, cesco <fd**********@g mail.comwrote:
>Hi,
I have a file containing four columns of data separated by tabs (\t) and I'd like to read a specific column from it (say the third). Is there any simple way to do this in Python?
I've found quite interesting the linecache module but unfortunately that is (to my knowledge) only working on lines, not columns.
Any suggestion?
the csv module may do what you want.
Here's an example:
>>print open("tmp.csv") .read()
alpha beta gamma delta
one two three for
>>records = csv.reader(open ("tmp.csv"), delimiter="\t") [record[2] for record in records]
['gamma', 'three']
Peter
On Jan 11, 4:15 am, cesco <fd.calabr...@g mail.comwrote:
Hi,
I have a file containing four columns of data separated by tabs (\t)
and I'd like to read a specific column from it (say the third). Is
there any simple way to do this in Python?
You say you would like to "read" a specific column. I wonder if you
meant read all the data and then just seperate out the 3rd column or
if you really mean only do disk IO for the 3rd column of data and
thereby making your read faster. The second seems more interesting
but much harder and I wonder if any one has any ideas. As for the
just filtering out the third column, you have been given many
suggestions already.
Regards,
Ivan Novick http://www.0x4849.net
Here is another suggestion:
col = 2 # third column
filename = '4columns.txt'
third_column = [line[:-1].split('\t')[col] for line in open(filename,
'r')]
third_column now contains a list of items in the third column.
This solution is great for small files (up to a couple of thousand of
lines). For larger file, performance could be a problem, so you might
need a different solution.
On Jan 17, 8:47 pm, Hai Vu <wuh...@gmail.c omwrote:
Here is another suggestion:
col = 2 # third column
filename = '4columns.txt'
third_column = [line[:-1].split('\t')[col] for line in open(filename,
'r')]
third_column now contains a list of items in the third column.
This solution is great for small files (up to a couple of thousand of
lines). For larger file, performance could be a problem, so you might
need a different solution.
Using the maxsplit arg could speed it up a little:
line[:-1].split('\t', col+1)[col] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Leeor Chernov |
last post by:
Hi ,
I am using the method:
DSSap.ReadXml( XmlPath,XmlReadMode.InferSchema );
And as I expected I get an error when the xml does not matching my DataSet that contains already a schema ,
The problem is that I get the following to-much detailed message:
System.FormatException: Input string was not in a correct format.
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s,...
|
by: The Cool Giraffe |
last post by:
I have a 3D matrix in MatLab and i have saved it binary
(i think) to a file data.mat and now i'd like to retrieve
those values in C++.
Even though i'm a ground breaking genius working with
extremely innovative frontier of science (joke intended)
i can imagine _SOMEBODY_ needed to perform such an
operation before me as well. Hence, i have a feeling
that there's an easy way to do that.
|
by: waynetheengineer |
last post by:
Hello everyone :)
I am trying to write VB code for reading filenames and file property values in a specific directory.
For example, I have a directory called C:/Bears and in that directory are three files: Brown Bears.txt, Black Bears.txt, and Orange Bears.txt. Each of these files have text descriptions in both the Title property field and the Comments property field of the each file (when you right click the file and select Properties,...
|
by: Problematic coder |
last post by:
Hi,
I have just been asked to create an app that will import a specific
column from an excel spreadsheet, something like sheet1 columnD or a
column with the first row with a value of 'ID' whichever would be
easier. The rest of what I need to do once I have the data I am ok
with but the connecting to the spreadsheet and extracting a column
into an array or dataset or whatever would work best I have no clue.
I would appreciate some...
|
by: convexcube |
last post by:
After searching the web for a solution to total a specific column in a list box and not finding it, I came up with this:
Dim varTotal As Currency
Dim varRow As Integer
For varRow = 1 To (lstListbox.ListCount - 1)
varTotal = varTotal + lstListbox.Column(2, varRow)
Next
txtTotal = varTotal
This will provide a total in txtTotal for the third column (referenced as "2" because the first column is "0") in the list box as currency.
| |
by: Derik |
last post by:
I've got a XML file I read using a file_get_contents and turn
into a simpleXML node every time index.php loads. I suspect this is
causing a noticeable lag in my page-execution time. (Or the wireless
where I'm working could just be ungodly slow-- which it is.)
Is reading a file much more resource/processor intensive than,
say, including a .php file? What about the act of creating a
simpleXML object? What about the act of checking the...
|
by: anatanat |
last post by:
hello :)
I want to bind a combobox to a specific column in a DataTable.
I want the combobox items to display distinct values.
I tried:
combo.datasource = dataTable;
combo.DisplayMember = "DESIRED COLUMN";
(here I got the values I want, but including empty rows, and with repetitions).
|
by: jbt007 |
last post by:
Hi All,
I am using the following code to import a spreadsheet into my Access 2003 tblWeekly table:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "tblweekly", strOutFile, True, "impData"
The strOutFile contains the path and file name of the spreadsheet. The "impData" is the range that contains the data I need to import. It is a fluid range (changes from week to week). The spreadsheet is created using Access Automation...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |