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 9247279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:41:18+00:00 2026-06-18T09:41:18+00:00

So, I wrote small test code for calculating the averages from all the csv

  • 0

So, I wrote small test code for calculating the averages from all the csv files in a folder.

parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()

# Get command arguments
input_files_path = args.input
output = args.output

avg = []
for infile in glob.glob(input_files_path+"*.csv"):
        a = np.loadtxt(infile, delimiter=",")
        mean = np.mean(a, axis=0)
        avg.append((mean))
        print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")

but the issue now, when I am trying to save the name of the file as well in the csv file like this:

parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()

# Get command arguments
input_files_path = args.input
output = args.output

avg = []
for infile in glob.glob(input_files_path+"*.csv"):
        a = np.loadtxt(infile, delimiter=",")
        mean = np.mean(a, axis=0)
        avg.append((infile,(mean)))
        print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")

it throws an error:

    np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
  File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 963, in savetxt
    X = np.asarray(X)
  File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 235, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: cannot set an array element with a sequence

I don’t seem to understand what the issue is. Maybe it is because I am trying to append a string to a float? I check the fmts in numpy.. but I did not find anything interesting.

/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py:773: UserWarning: loadtxt: Empty input file: "../robalo-31.1/op.csv"
  warnings.warn('loadtxt: Empty input file: "%s"' % fname)
/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py:2374: RuntimeWarning: invalid value encountered in double_scalars
  return mean(axis, dtype, out)
  • 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-18T09:41:19+00:00Added an answer on June 18, 2026 at 9:41 am

    I’m guessing there’s a problem with the format characters you’re providing with np.savetxt. (you have a string-float tuple and you’re delimiter implies you’re only using floats). I would suggest csv for this instead:

    >>> import csv
    >>> import StringIO
    >>> g = StringIO.StringIO()
    >>> writer = csv.writer(g,delimiter=',')  #this is the part that is applicable
    >>> writer.writerows(lst)
    >>> g.seek(0)
    >>> print g.read()
    foo,2
    bar,4
    

    So I’m guessing your code could be simply:

    with open(output,'wb') as fout:
        writer = csv.writer(fout,delimiter=',') 
        writer.writerows(avg)
    

    instead of

    np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code for a small program I just wrote to test some
I wrote this small piece of code in C to test memcmp() strncmp() strcmp()
Have small web page at www.peterbio.com/mom/test.htm Someone wrote the code with mouse over and
I wrote a small Java command line program to test sending emails from a
I wrote this small piece of code to test something: #include <stdio.h> int main()
I wrote a small script to read from multiple excel files and write the
I wrote a small test program for flipperview. I have 3 views. I call
I imported LuaInterface into a console project, referenced it, and wrote a small test
I wrote a small program to test accessing a widget parent's slot. Basically, it
I wrote two small applications (a client and a server) to test UDP communication

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.