Import multiples files txt

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Import multiples files txt

Javier Meneses
I have multiples files csv format sequencial that begin with baci02_1995 to baci02_2014, and I want to open it and save as sav format. 

    I founded the next syntax in internet but no work to me well (just I changed the path drive).


Begin Program.
import spss

for x in range (1995, 2014):

   y = """GET DATA  /TYPE=TXT
     /FILE= 'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\ baci """ + str(x) + """.csv'
      /ENCODING='Locale'
     /DELCASE=LINE
     /DELIMITERS=" ,"
     /ARRANGEMENT=DELIMITED
     /FIRSTCASE=2
     /IMPORTCASE=ALL
     /VARIABLES=
    t F4.0
    hs6 F6.0
    i F3.0
    j F3.0
    v F12.10
    q F10.3."""

   z = "save outfile = 'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\sav\ baci " + str(x) + ".sav'."

   print y
   print z
   spss.Submit(y)
   spss.Submit(z)

End Program.


         Where is the mistake??  Can you help me??  Is there other way to do this?

                            Thank , Javier
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Import multiples files txt

Jon Peck
Assuming the csv specifications are correct, the first thing to do is to protect the \ characters, which have a special meaning in a Python string in some cases.

Put an r before each instance of a path containing \, e.g.,
/FILE=r'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\ baci """ + str(x) + """.csv'

Alternatively, write the path separators as / instead of \.  Statistics accepts both.

On Wed, Oct 12, 2016 at 7:52 AM, javier meneses <[hidden email]> wrote:
I have multiples files csv format sequencial that begin with baci02_1995 to baci02_2014, and I want to open it and save as sav format. 

    I founded the next syntax in internet but no work to me well (just I changed the path drive).


Begin Program.
import spss

for x in range (1995, 2014):

   y = """GET DATA  /TYPE=TXT
     /FILE= 'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\ baci """ + str(x) + """.csv'
      /ENCODING='Locale'
     /DELCASE=LINE
     /DELIMITERS=" ,"
     /ARRANGEMENT=DELIMITED
     /FIRSTCASE=2
     /IMPORTCASE=ALL
     /VARIABLES=
    t F4.0
    hs6 F6.0
    i F3.0
    j F3.0
    v F12.10
    q F10.3."""

   z = "save outfile = 'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\sav\ baci " + str(x) + ".sav'."

   print y
   print z
   spss.Submit(y)
   spss.Submit(z)

End Program.


         Where is the mistake??  Can you help me??  Is there other way to do this?

                            Thank , Javier
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Import multiples files txt

Albert-Jan Roskam-3
Hi Javier,

You're skipping the last year.

File handle path /name = 'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii'.
CD path.
Begin program.
import spss
cmd = r"""get data /type=text /file='baci℅s.csv' etc etc.
Save outfile = 'sav\baci℅s.sav'.
"""
spss.Submit( [cmd % (year, year) for year in range(1994,2014+1)] )
End program.
From: SPSSX(r) Discussion <[hidden email]> on behalf of Jon Peck <[hidden email]>
Sent: Wednesday, October 12, 2016 1:57:44 PM
To: [hidden email]
Subject: Re: [SPSSX-L] Import multiples files txt
 
Assuming the csv specifications are correct, the first thing to do is to protect the \ characters, which have a special meaning in a Python string in some cases.

Put an r before each instance of a path containing \, e.g.,
/FILE=r'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\ baci """ + str(x) + """.csv'

Alternatively, write the path separators as / instead of \.  Statistics accepts both.

On Wed, Oct 12, 2016 at 7:52 AM, javier meneses <[hidden email]> wrote:
I have multiples files csv format sequencial that begin with baci02_1995 to baci02_2014, and I want to open it and save as sav format. 

    I founded the next syntax in internet but no work to me well (just I changed the path drive).


Begin Program.
import spss

for x in range (1995, 2014):

   y = """GET DATA  /TYPE=TXT
     /FILE= 'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\ baci """ + str(x) + """.csv'
      /ENCODING='Locale'
     /DELCASE=LINE
     /DELIMITERS=" ,"
     /ARRANGEMENT=DELIMITED
     /FIRSTCASE=2
     /IMPORTCASE=ALL
     /VARIABLES=
    t F4.0
    hs6 F6.0
    i F3.0
    j F3.0
    v F12.10
    q F10.3."""

   z = "save outfile = 'C:\Users\JMENESES2\Documents\Comercio-carpeta de trabajo\Nano\datos\Cepii\sav\ baci " + str(x) + ".sav'."

   print y
   print z
   spss.Submit(y)
   spss.Submit(z)

End Program.


         Where is the mistake??  Can you help me??  Is there other way to do this?

                            Thank , Javier
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD