Haskell GHCi - 使用getContents在stdin上使用EOF字符(Haskell GHCi - Using EOF character on stdin with getContents)
我喜欢通过粘贴到解释器来解析Python中的特殊字符串。
>>> s = """Adams, John ... Washington,George ... Lincoln,Abraham ... Jefferson, Thomas ... """ >>> print "\n".join(x.split(",")[1].replace(" ", "") for x in s.strip().split("\n")) John George Abraham Thomas
这很适合使用Python解释器,但我想用Haskell / GHCi来做这件事。 问题是,我无法粘贴多行字符串。 我可以使用带有EOF字符的getContents,但是我只能执行一次,因为EOF字符关闭stdin。
Prelude> s <- getContents Prelude> s "Adams, John Adams, John\nWashington,George Washington,George\nLincoln,Abraham Lincoln,Abraham\nJefferson, Thomas Jefferson, Thomas\n^Z " Prelude> :{ Prelude| putStr $ unlines $ map ((filter (`notElem` ", ")) Prelude| . snd . (break (==','))) $ lines s Prelude| :} John George Abraham Thomas Prelude> x <- getContents *** Exception: <stdin>: hGetContents: illegal operation (handle is closed)
使用GHCi有更好的方法吗? 注意 - 我对getContents(以及一般的Haskell IO)的理解可能会严重破坏。
更新
我会玩我收到的答案。 下面是我制作的一些帮助函数(剽窃),它模仿了Python的
"""
引用(以"""
结尾,而不是从”ephemient“的答案结束)。getLinesWhile :: (String -> Bool) -> IO String getLinesWhile p = liftM unlines $ takeWhileM p (repeat getLine) getLines :: IO String getLines = getLinesWhile (/="\"\"\"")
在GHCi中使用AndrewC的答案 -
C:\...\code\haskell> ghci HereDoc.hs -XQuasiQuotes ghci> :{ *HereDoc| let s = [heredoc| *HereDoc| Adams, John *HereDoc| Washington,George *HereDoc| Lincoln,Abraham *HereDoc| Jefferson, Thomas *HereDoc| |] *HereDoc| :} ghci> putStrLn s Adams, John Washington,George Lincoln,Abraham Jefferson, Thomas ghci> :{ *HereDoc| putStr $ unlines $ map ((filter (`notElem` ", ")) *HereDoc| . snd . (break (==','))) $ lines s *HereDoc| :} John George Abraham Thomas
I like to parse strings ad hoc in Python by just pasting into the interpreter.
>>> s = """Adams, John ... Washington,George ... Lincoln,Abraham ... Jefferson, Thomas ... """ >>> print "\n".join(x.split(",")[1].replace(" ", "") for x in s.strip().split("\n")) John George Abraham Thomas
This works great using the Python interpreter, but I'd like to do this with Haskell/GHCi. Problem is, I can't paste multi-line strings. I can use getContents with an EOF character, but I can only do it once since the EOF character closes stdin.
Prelude> s <- getContents Prelude> s "Adams, John Adams, John\nWashington,George Washington,George\nLincoln,Abraham Lincoln,Abraham\nJefferson, Thomas Jefferson, Thomas\n^Z " Prelude> :{ Prelude| putStr $ unlines $ map ((filter (`notElem` ", ")) Prelude| . snd . (break (==','))) $ lines s Prelude| :} John George Abraham Thomas Prelude> x <- getContents *** Exception: <stdin>: hGetContents: illegal operation (handle is closed)
Is there a better way to go about doing this with GHCi? Note - my understanding of getContents (and Haskell IO in general) is probably severely broken.
UPDATED
I will be playing with the answers I have received. Here are some helper functions I made (plagiarized) that simulate Python's
"""
quoting (by ending with"""
, not starting) from ephemient's answer.getLinesWhile :: (String -> Bool) -> IO String getLinesWhile p = liftM unlines $ takeWhileM p (repeat getLine) getLines :: IO String getLines = getLinesWhile (/="\"\"\"")
To use AndrewC's answer in GHCi -
C:\...\code\haskell> ghci HereDoc.hs -XQuasiQuotes ghci> :{ *HereDoc| let s = [heredoc| *HereDoc| Adams, John *HereDoc| Washington,George *HereDoc| Lincoln,Abraham *HereDoc| Jefferson, Thomas *HereDoc| |] *HereDoc| :} ghci> putStrLn s Adams, John Washington,George Lincoln,Abraham Jefferson, Thomas ghci> :{ *HereDoc| putStr $ unlines $ map ((filter (`notElem` ", ")) *HereDoc| . snd . (break (==','))) $ lines s *HereDoc| :} John George Abraham Thomas
原文:https://stackoverflow.com/questions/12119420
最满意答案
演示网址: -
http://jquery.malsup.com/form/progress.html
你可以从这个URL下载jQuery文件并添加html
<head>
标签http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js
http://malsup.github.com/jquery.form.js
尝试这个:
这是我的HTML标记:
<!doctype html> <head> <title>File Upload Progress Demo #1</title> <style> body { padding: 30px } form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px } .progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; } .bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; } .percent { position:absolute; display:inline-block; top:3px; left:48%; } </style> </head> <body> <h1>File Upload Progress Demo #1</h1> <code><input type="file" name="myfile"></code> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="uploadedfile"><br> <input type="submit" value="Upload File to Server"> </form> <div class="progress"> <div class="bar"></div > <div class="percent">0%</div > </div> <div id="status"></div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.github.com/jquery.form.js"></script> <script> (function() { var bar = $('.bar'); var percent = $('.percent'); var status = $('#status'); $('form').ajaxForm({ beforeSend: function() { status.empty(); var percentVal = '0%'; bar.width(percentVal) percent.html(percentVal); }, uploadProgress: function(event, position, total, percentComplete) { var percentVal = percentComplete + '%'; bar.width(percentVal) percent.html(percentVal); }, complete: function(xhr) { bar.width("100%"); percent.html("100%"); status.html(xhr.responseText); } }); })(); </script> </body> </html>
我的php代码:
<?php $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?>
Demo url:--
http://jquery.malsup.com/form/progress.html
You can download jQuery files from this url and add in html
<head>
taghttp://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js
http://malsup.github.com/jquery.form.js
Try this:
This is my html markup:
<!doctype html> <head> <title>File Upload Progress Demo #1</title> <style> body { padding: 30px } form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px } .progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; } .bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; } .percent { position:absolute; display:inline-block; top:3px; left:48%; } </style> </head> <body> <h1>File Upload Progress Demo #1</h1> <code><input type="file" name="myfile"></code> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="uploadedfile"><br> <input type="submit" value="Upload File to Server"> </form> <div class="progress"> <div class="bar"></div > <div class="percent">0%</div > </div> <div id="status"></div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.github.com/jquery.form.js"></script> <script> (function() { var bar = $('.bar'); var percent = $('.percent'); var status = $('#status'); $('form').ajaxForm({ beforeSend: function() { status.empty(); var percentVal = '0%'; bar.width(percentVal) percent.html(percentVal); }, uploadProgress: function(event, position, total, percentComplete) { var percentVal = percentComplete + '%'; bar.width(percentVal) percent.html(percentVal); }, complete: function(xhr) { bar.width("100%"); percent.html("100%"); status.html(xhr.responseText); } }); })(); </script> </body> </html>
My php code:
<?php $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?>
相关问答
更多-
通过本地客户端程序获取服务器mysql数据库信息[2023-04-27]
ajax -
尝试替换代码: data = new FormData($('#form')); 有了这个: data = new FormData($('#form')[0]); 从jQuery数组中获取第一个DOM元素。 Try to replace the code: data = new FormData($('#form')); with this: data = new FormData($('#form')[0]); to get the first DOM element from the jQue ...
-
FormData构造函数将HTMLFormElement作为参数而不是选择器字符串或jQuery对象。 data: new FormData(document.getElementById('info_for')), 要么 data: new FormData($('#info_for')[0]), 要么 data: new FormData(document.querySelector('#info_for')), The FormData Constructor takes an HTMLForm ...
-
你可以使用这个插件: http://aquantum-demo.appspot.com/file-upload 要么 http://valums.com/files/2010/file-uploader/demo.htm 要么 http://www.uploadify.com/demos/ you can use this plugins : http://aquantum-demo.appspot.com/file-upload or http://valums.com/files/2010/file-u ...
-
演示网址: - http://jquery.malsup.com/form/progress.html 你可以从这个URL下载jQuery文件并添加html 标签 http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js http://malsup.github.com/jquery.form.js 尝试这个: 这是我的HTML标记:
File Upload Progress D ... -
PHP + Ajax:通过Ajax上传大型内容(413 HTTP代码)(PHP + Ajax: Uploading large content via Ajax (413 HTTP Code))[2022-10-06]
对于将来遇到类似问题的人,我在这个问题的答案中找到了答案 基本上我们将使用以下命令将base64字符串作为blob添加到formData对象: var formData = new FormData(); var blob = new Blob(['Lorem ipsum'], { type: 'plain/text' }); formData.append('file', blob,'readme.txt'); 这似乎模仿了文件输入的行为。 For anyone having similar probl ... -
我用这种方式解决了这个问题。您正在使用名称file发送图像 $_FILES['file'] 代替 $_FILES['LogoImageUploader'] 或者改变js formData.append('LogoImageUploader', $('#LogoImageUploader')[0].files[0]); You are sending image by name file so use $_FILES['file'] Instead of $_FILES['LogoImageUploader'] Or ...您可以使用表单数据传递多个文件,如下所示 HTML JS var fd = new FormData(); var files = $("#fuDocument").get(0).files; // this is my file input in which We can select multiple files. fd.append("label", ...