-1

My code name is Test.py and it contains:

#! /usr/bin/python3.8
import sys                          
import numpy as np                  
from numpy import loadtxt           
from numpy import savetxt
import math                         
import pandas as pd                 

filename = open(sys.argv[1], "r")
Matriz = loadtxt(sys.argv[1], comments="#", delimiter=",", unpack=False)
filename.close()

filename = open(sys.argv[1], "r")
Atomos = loadtxt(sys.argv[1], comments="#", delimiter=",", unpack=False)
filename.close()

print(np.array(Matriz))
print(np.array(Atomos))

The content of both files is:

Matriz

-0.0348687198,0.1076326826,0.0000843068
0.0573782020,1.3095730862,0.0000068272
1.0254494561,-0.7164980418,0.0000085392
2.2996131174,-0.0921357246,-0.0000447488
3.0330746822,-0.8942281572,-0.0001369182
2.4148217507,0.5343388106,-0.8871728184
2.4149308325,0.5342246938,0.8871497899
-1.2975203980,-0.6615612550,0.0000051853
-2.4725470102,-0.0497367819,-0.0000446353
-3.4040644912,-0.5890411227,0.0000073839
-2.5208587260,1.0272815633,-0.0001277685
-1.1879865504,-1.7352844954,0.0000351158

Atomo

C
O
O
C
H
H
H
C
C
H
H
H

and I ran my code like: ./Test.py Matriz Atomo' and it only print the content of Matriz.

How can I make my code to read two or more files in command line?

I tried it:

filename = open(sys.argv[1], "r")
Matriz = loadtxt(sys.argv[1], comments="#", delimiter=",", unpack=False)
filename.close()

filename = open(sys.argv[2], "r")
Atomos = loadtxt(sys.argv[2], comments="#", delimiter=",", unpack=False)
filename.close()

and I get:

Traceback (most recent call last):
  File "./E_State.py", line 15, in <module>
    Atomos = loadtxt(sys.argv[2], comments="#", delimiter=",", unpack=False)
  File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 1146, in loadtxt
    for x in read_data(_loadtxt_chunksize):
  File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 1074, in read_data
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 1074, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 781, in floatconv
    return float(x)
ValueError: could not convert string to float: 'C'
martineau
  • 119,623
  • 25
  • 170
  • 301
Another.Chemist
  • 2,386
  • 3
  • 29
  • 43

1 Answers1

1
filename = open(sys.argv[1], "r")
Matriz = loadtxt(sys.argv[1], comments="#", delimiter=",", unpack=False)
filename.close()

filename = open(sys.argv[2], "r")
Atomos = loadtxt(sys.argv[2], comments="#", delimiter=",", unpack=False)
filename.close()
# change one of these to 2
# python test.py Matriz Atomos

If the text file is indentical to what was posted then change Atomos to

import pandas as pd
Atomos = pd.read_csv(sys.argv[2])
fthomson
  • 773
  • 3
  • 9