Counting number of columns in text file with Python

2024/10/9 7:18:50

I have two text files composed of spaced-separated columns. These are excerpts of these two files:

FileA

 1 1742.420   -0.410  20.1530   0.4190   1.7080   0.59402 1872.060    0.070  21.4710   0.2950   0.0670   0.33803 1918.150    0.150  18.9220   0.0490   1.4240   0.11504 1265.760    0.170  19.0850   0.0720   1.3330   0.14505  308.880    0.220  20.5020   0.1570   0.0200   0.1720....

FileB

 1 1198.367    6.465  15.684 0.015  3.119 0.140  12 1451.023    6.722  17.896 0.031  0.171 0.041  13 1032.364    6.788  18.895 0.074 -0.084 0.088  14  984.509    7.342  19.938 0.171  0.043 0.322  15 1068.536    7.369  19.182 0.091  0.486 0.143  1....

As you can see FileA has 7 columns and FileB has 8. This is the code I use to count the columns:

import csv
file = 'filename'
reader = csv.reader(file)
num_cols = 0
for col in reader:num_cols = num_cols + 1

This little code correctly gives the number of columns for FileA (7) but not for FileB (also gives 7) What's going on and how can I fix it?

If I'm counting rows instead of columns then: 1- how can I count the columns? and 2- given that my actual files have several thousands of lines/rows, why am I getting these results (7)?

Answer
import csvwith open('filename') as f:reader = csv.reader(f, delimiter=' ', skipinitialspace=True)first_row = next(reader)num_cols = len(first_row)
https://en.xdnf.cn/q/70044.html

Related Q&A

Django on Apache web server dict object has no attribute render_context

Im having a bit of a problem, I uploaded my Django project to a webserver running apache, mod_python, and django. On the computer I developed on the following works finenameBox = getNamesBox().render(l…

Verbose log abbriviations meaning in SVC, scikit-learn

I am looking for the meaning of verbose log abbriviations of SVC function in scikit-learn?If nSV is the number of support vectors, #iter is the number of iteration, what dose nBSV, rho,obj mean?This …

networkx draw_networkx_edges capstyle

Does anyone know if it is possible to have fine-grained control over line properties when drawing networkx edges via (for example) draw_networkx_edges? I would like to control the line solid_capstyle …

Setting up the path so AWS cli works properly

I installed AWSCLI by using:pip install --upgrade --user awscliNow if I type aws configure in the cmd I get: aws is not recognized as an internal or external command...Im pretty sure the path needs to …

Comparing value with neighbor elements in numpy

Lets say I have a numpy arraya b c A = i j ku v wI want to compare the value central element with some of its eight neighbor elements (along the axis or along the diagonal). Is there any faster way exc…

Is Python bad at XML? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Conditional color with matplotlib scatter

I have the following Pandas Dataframe, where column a represents a dummy variable:What I would like to do is to give my markers a cmap=jet color following the value of column b, except when the value i…

Cumulative sum with list comprehension

I have a list of integers:x = [3, 5, 2, 7]And I want to create a new list where the nth element is the sum of elements in x from 0 to n-1.This would result in:y = [0, 3, 8, 10]How can I do this with li…

How to get image size in KB while using Pillow-python before storing to disk?

Im using Pillow for image processing in Python,url="http://www.image.com/some_image.jpg";path = io.BytesIO(urllib.request.urlopen(url).read())original_image = Image.open(path)Any idea how i c…

Python: Print only one time inside a loop

I have a code where I want capture a video from a camera. I want to use Logging library of Python to get messages on the shell or export them to a text file.Here is a part of my code where inside the w…