• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

write stdout using write_csv() ...

Question

tarifa    2

dear Fellow Coder on Neowin 

 

 

im using bash to pipe the gathered data through an Rscript like so:

 

cat random.csv | Rscript test.R arg >| delete.csv

 

My aim is to use the R package readr to both read stdin and write stdout. what is aimed:  I found the answer to stdin here.

 

test.R

#!/usr/bin/Rscript
suppressMessages(library(readr))

args  <- commandArgs(trailingOnly = TRUE)

df.in <- read_csv(file("stdin"))

write_csv(df.in, path = stdout())

 


My investigations gave back the foolowing: 

There is a format_csv function for that in readr. Use this instead of write_csv:

cat(format_csv(df.in))

 

 

we also can use write.table:

 

write.table(x, file = "foo.csv", sep = ",", col.names = NA,
        
        qmethod = "double")

 

 

i found the following interesting help page: https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html 

 

import requests
from bs4 import BeautifulSoup
import pandas as pd


def Main(urls):
    with requests.Session() as req:
        allin = []
        for url in urls:
            r = req.get(url)
            soup = BeautifulSoup(r.content, 'html.parser')
            target = soup.find(
                "dl", class_="c-description-list c-description-list--striped")
            names = [item.text for item in target.findAll("dt")]
            names.append("url")
            data = [item.get_text(strip=True) for item in target.findAll("dd")]
            data.append(url)
            allin.append(data)
        df = pd.DataFrame(allin, columns=names)
        df.to_csv("data.csv", index=False, encoding="utf-8")

urls = ['https://www2.daad.de/deutschland/studienangebote/international-programmes/en/detail/4722/',
        'https://www2.daad.de/deutschland/studienangebote/international-programmes/en/detail/6318/']
Main(urls)
 

 

the question is. i want to write the results of the parser script to stdout . - i do not want to write it into the data-file.

 

 


Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.