extract a column from text file

2024/11/20 13:27:07

I have a a text file (huge amount of float numbers) with 25 columns. I want to extract column 14 and divide it by column 15. I could not extract this two columns.
Codes:

with open('sample for north.txt') as infile:for line in infile:print(line.split()[13])

Error : list index out of range

Answer

You are getting the error Error : list index out of range because there aren't enough columns (at least on the given line). It's better to check, something along this line:

with open('sample for north.txt') as infile:for line in infile:parts = line.split()if len(parts) > 13: # or whatever is appropriateprint(parts[13])

Explanation: when you split a line, it returns a list of items. E.g., if there were 3 columns, .split() would return list containing 3 items. The length of the list varies with each line of course depending on the data.

Your code assumed that there always were the required number of items on a given line and tried to access the item in the list at index 13. However, there must be at least one line in your data file where this is not the case, which is why your code crashed. Therefore it's better to examine the length of the list before trying to access a given index in the list.

I.e., I split the line into its "parts", and then examined its length before trying to access it.

https://en.xdnf.cn/q/118414.html

Related Q&A

kivy buildozer Compile Error pythonforandroid.toolchain

Compile platformCommand failed: /usr/bin/python3 -m pythonforandroid.toolchain create --dist_name=main -- bootstrap=sdl2 --requirements=kivy,python3 --arch armeabi- v7a --copy-libs --color=always --…

Django Error: No FlatPage matches the given query

SITE_ID = 1and (r, include(django.contrib.flatpages.urls)), is in urls.py.What can I do to fix this error? Django is still displaying this error - I have googled and I cant find anything.File urls.pyf…

I need to automate the filling of a HTML form in a web browser, how?

I am trying to build a python script that captures my screen (a website will be opened), finds the coordinates of a text entry box on the displayed web site, and then clicks in that text entry box. I a…

Page not found (404) at /user_signup in Django

Getting 404 error on signup and some more URLs. /login, /logout, /admin is working perfectly. Im making a web app that lets a user login, logout, search a flight, book a flight and view the bookings ma…

tensorflow:Your input ran out of data when using custom generator

I am using custom generator to pass my data. But i keep encountering an error which says i have run out of data and to use repeat() when passing the dataset. i am using plain generator therefore it is …

script to get the max from column based on other column values

I need a script to read in a csv file(orig.csv) and output a reformatted csv file(format.csv) The orig csv file will look like this: Time,Label,frame,slot,SSN,Board,BT,SRN,LabelFrame,SRNAME,LabelID,Int…

Selenium code is not able to scrape ofashion.com.cn

I was building a web scraper by using python selenium. The script scraped sites like amazon, stack overflow and flipcart but wasnt able to scrape ofashion. It is always returning me a blank .csv file.H…

How can I access each estimater in scikit-learn pipelines? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How do I structure a repo with Cloud Run needing higher level code?

I have added code to a repo to build a Cloud Run service. The structure is like this:I want to run b.py in cr. Is there any way I can deploy cr without just copying b.py into the cr directory? (I dont…

Unable to build kivy image loaded .py file into exe using auto-py-to-exe

I have a simple kivy file in which i want to cover the entire canvas with an image bgi.jpg MainWidget: <MainWidget>:canvas.before:Rectangle:size:self.sizesource:bgi.jpgand the .py file code i…