exporting different lists to .txt in python

2024/10/15 10:12:56

I have a few lists which I all want to export to the same .txt file.

So far I only export 3 of the lists using

my_array=numpy.array(listofrandomizedconditions)
my_array2=numpy.array(inputsuser)
my_array3=numpy.array(reactiontimesuser)
combined=numpy.column_stack([my_array,my_array2,my_array3])
numpy.savetxt(participantnumber + ".txt", combined, delimiter=" ", fmt ="%-12s") 

This gives me an output like

CongruentPositief no input or wrong button no reactiontime
IncongruentNegPos no input or wrong button no reactiontime

Since this is quite hard to read I want to add a tab between all the different lists.

Also I want to add a few lists which aren't 192 elements long unlike the first 3, but then I get an error that every array has to be the same size. Is there a way around this?

Answer

Especially if you are starting with lists (of strings) I don't see the point to using numpy arrays.

For a start just try printing values:

In [659]: conditions=['one','two','three']
In [660]: values=[1,2,3]
In [661]: other=['xxxx','uuuuuuu','z']

basic format

In [662]: for xyz in zip(conditions, values,other):print("%s,%s,%s"%xyz).....:     
one,1,xxxx
two,2,uuuuuuu
three,3,z

refined with tab and fixed lengths:

In [663]: for xyz in zip(conditions, values,other):print("%-12s\t%-12s\t%-12s"%xyz).....:     
one             1               xxxx        
two             2               uuuuuuu     
three           3               z     

Next step is to open a file and write to that, instead of print.

It's column stack that requires equal length strings. savetxt just creates a fmt string from your parameter (and the number of columns), and writes each row like I do.


In [667]: with open('temp.txt','w') as f:.....:     for xyz in zip(conditions,values,other):.....:         f.write('%-12s,%-12s,%-12s\n'%xyz).....:         
In [668]: cat temp.txt
one         ,1           ,xxxx        
two         ,2           ,uuuuuuu     
three       ,3           ,z  
https://en.xdnf.cn/q/117843.html

Related Q&A

Retrieving information from dictionary

Im having hard time trying to read my dictionary variable. Python keeps throwing the following error:TypeError: string indices must be integersThis is a sample that should give you an idea of what my p…

Python WMI Hyper-v GetSummaryInformation result

Im trying to retrieve information from all the available VMs on a Hyper-V Server. The problem is that when I ask for the summary information, i get a list of useless COMObjects.I cant find a way of get…

How to dynamically change variable name in form.vars.var_name

I have defined counter variable in controller.I can define tables and fields dynamically.tables = [db.define_table(example_table_%s % x,Field(example_field_%s % x, type=string, ...)...)for x in range(0…

Why will one loop modify a list of lists, but the other wont [duplicate]

This question already has answers here:Python list doesnt reflect variable change(6 answers)Closed 8 years ago.One of the answers in "python way" to parse and conditionally replace every elem…

Python CGI executable script downloads / shows script code

A script I wrote long ago (getWords.py) used to be executable at my localhost (http://local.example.com/getWords.py?query-string)My python script starts like this :#!/usr/bin/env python # chmod 755 ge…

My function returns None

I am new to Python and I was trying to solve this exercise, but keep getting None output. The question asked for a program in which the input is hours and rate and the output is the gross pay, includin…

Django undefined symbol: PyUnicode_AsUTF8

I am new to Python/Django. I have set up the environment needed to run Django project.When Im trying to migrate an existing project , it shows up this errordjango.core.exceptions.ImproperlyConfigured: …

using a variable keyword for an optional argument name with python argparse

I am using argparse for a python script I am writing. The purpose of the script is to process a large ascii file storing tabular data. The script just provides a convenient front-end for a class I have…

Error while setting up MongoDB with django using django mongodb engine on windows

Steps I followed :pip install git+htp://github.com/django-nonrel/[email protected]It did not work, so I downloaded the zip from the site "htp://github.com/django-nonrel/django" and pasted the…

Python login page with pop up windows

I want to access webpages and print the source codes with python, most of them require login at first place. I have similar problem before and I have solved it with the following code, because they are…