'R: Loop an extraction .xml files inside of .zip files?

I created a database (url_list) with a unique variable containing URLs (myurl). These URLs allow to download .zip files. I would like to create a loop for all these URLs that extract a .xml document inside of each .zip file.

The code begins as follow:

for(myurl in url_list){
  temp <- tempfile()
  download.file(myurl, temp)
  num <- substr(myurl, 43, 55)
  file <- xmlParse(unz(temp, "num.xml"))
  unlink(temp)
}

The first problem that I have is that the name of the file that I want to extract inside the .zip file depends of a number in the url. For instance, for a given URL, num = 1354V1000001@ and so the file inside is called [email protected]. But since it is in "", it doesn't recognize it.

Please, I need your help for improvements, and to create a code that works :)



Solution 1:[1]

  • One way is to redirect logs to NullHandler (python >= 3.1)
#!/bin/bash

var=$(python3 <<EOF
import logging
logging.basicConfig(handlers=[logging.NullHandler()])
print("usefull text")
logging.getLogger().critical("critical")
logging.getLogger().error("an error")
logging.getLogger().info("a message")
EOF
)

echo "var is: $var"
  • Or for python2:
#!/bin/bash

var=$(python2 <<EOF
import logging
logging.basicConfig(filename="/dev/null")
print("usefull text")
logging.getLogger().critical("critical")
logging.getLogger().error("an error")
logging.getLogger().info("a message")
EOF
)

echo "var is: $var"
  • By default python only log errors and criticals to stderr, so you can redirect stderr:
#!/bin/bash

var=$(python3 <<EOF 2>/dev/null
import logging
print("usefull text")
logging.getLogger().critical("critical")
logging.getLogger().error("an error")
logging.getLogger().info("a message")
EOF
)

echo "var is: $var"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1