Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:56:27+00:00 2026-06-04T20:56:27+00:00

Trying to write a fourth column to a set of data which looks like

  • 0

Trying to write a fourth column to a set of data which looks like this

 8000.5   16745     0.1257
 8001.0   16745     0.1242
 8001.5   16745     0.1565
 8002.0   16745     0.1595

Which has the number of times the second number (i.e. 16745) has been counted in that particular file (it does change, the list has a couple thousand entries). i.e. if this were the whole file

 8000.5   16745     0.1257   4
 8001.0   16745     0.1242   4
 8001.5   16745     0.1565   4
 8002.0   16745     0.1595   4

The problem with my code seems to be in the writing stage, the dictionary works, and csv.reader is reading the file fine if I print it but when it comes to appending the only dictionary key it picks up (field[1]) seems to be one where 16745 is -1 and the count of this is printed in the fourth column for all rows. I can’t understand why it is cross referencing with the dictionary only for this value and not on a per row basis.

i.e. I get

 8000.5   16745     0.1257   [count of -1 in column 2]
 8001.0   16745     0.1242   [count of -1 in column 2]
 8001.5   16745     0.1565   [count of -1 in column 2]
 8002.0   16745     0.1595   [count of -1 in column 2]

Any help would be greatly appreciated!

import numpy
import string
import csv
import sys
import os

time = []
water = []
itemcount ={}

global filename
filename = sys.argv[1]

f1 = open(sys.argv[1], 'rt')
for line in f1:
    fields = line.split()
    time.append(fields[0])
    water.append(fields[1])
f1.close()

for x in water:
    a = water.count(x)
    itemcount[x] = a

writerfp = open('watout.csv', 'w')
writer = csv.writer(writerfp)
for row in csv.reader(open(filename, 'r')):
    fields = line.split()
    row.append(itemcount[fields[1]])
    writer.writerow(row)
writerfp.close()    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:56:29+00:00Added an answer on June 4, 2026 at 8:56 pm

    The reason for your error is in the last loop. You should drop the line

    fields = line.split()
    

    and change the next line to

    row.append(itemcount[row[1]])
    

    Your code has a few more issues:

    1. You declare filename as global at global scope. This is meaningless, since it would be global anyway. Moreover, the next line in the code uses sys.argv[1] again.

    2. You should use with statements to open files.

    3. There is no t mode when opening files in Python 2.x.

    4. Your algorithm to determine the counts is very inefficient. You are iterating over the whole list for each entry of the list. You can make do in a single pass.

    Cleaning up all these issues and removing all the unused variables, you can get the job done with the following code:

    import collections
    with open(sys.argv[1], "r") as input:
        counts = collections.Counter(line.split()[1] for line in input)
        input.seek(0)
        with open("watout.csv", "w") as output:
            for line in input:
                count = counts[line.split()[1]]
                output.write(line.rstrip("\n") + "\t" + str(count) + "\n")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying write a function that generates simulated data but if the simulated
Trying to write a chat, like on facebook, I wondered if two clients can
I'm trying to figure out how to write a code which takes an input
I'm trying to write something like Perl Audio Converter , so I need to
Im trying to write an if function which checks the width of an image,
im trying to write out divs every fourth row but im in some kinda
I am trying to write a regex which will validate the format of a
I am trying to write some code which identifies the greatest two values for
Trying to write a RE to recognize date format mm/dd in Python reg =
Trying to write a simple jQuery function that will multiple the index of an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.