解析一个cv文件 [英] parse a cv file
问题描述
我的代码不能编译。
我试图解析一个看起来像这样的文件:
auiauiaui@tsrntsr.epep 584756srtrtte
jepeto@pinokio.disney 464456454cendrillon
如果您的姓名文件大声笑和代码前卫,然后调用前卫1 2将返回464456454.
编辑:
谢谢,
看到我有一个名为filetest的文件,像这样:
field1-1 field1-2 field1-3
field2-1 field2-2 field2-3
field3-1 field3-2 field3-3
field4-1 field4-2 field4-3 />
field5-1 field5-2 field5-3
现在我的一段代码是假设抓住一个给定行和等级。
mycode filetest给我文件中的行数(这样我就可以为每一行使用一个脚本)。
这里例如:
mycode filetest 1 2 - > field1-2
mycode filetest 2 2 - > field2-2
mycode filetest 3 4 - > field3-4
mycode filetest 3 2 - > field3-2
mycode filetest 2 5 - > field2-5
mycode filetest 1 4 - > field1-4
现在每个字段都包含一个String。 >
我所做的是将文件作为字符串抓取,然后抓住想要的行(用行),然后抓住字段(用单词)。
我纠正了我的错误(感谢#haskell和#haskell-fr频道和你)。但是我的代码并没有像预期的那样工作。
任何帮助将不胜感激。这里是新代码:
import System.Environment
import System.IO
fieldArg :: Int - > [字符串] - >字符串
fieldArg x a =如果长度a> 2
那么如果x < 4然后一个! x else字段号太高
else字段号太低
lineArg :: Int - > [字符串] - >字符串
lineArg x a
| x>长度a =行号太大
| x < 1 =行号太低
|否则= a! x
extrcta :: Int - > Int - >字符串 - >字符串
extrcta x n = fieldArg x。单词。 lineArg n。行
argList :: String - > [((1,n),extrcta 1 n1)中的[((Int,String),String - > String)]
argList n = let n1 = read n
(2,n),extrcta 2 n1)
,((3,n),extrcta 3 n1)
]
main :: IO()
main = do
args< - getArgs
(path:opt1:opt2:_) - > case(read opt1,opt2)`lookup` argList opt2 of
Nothing - > putStrLn $错误的参数++ opt1 ++或++ opt2
只要采取行动 - > (\handle-> do
contents< - hGetContents handle
putStrLn $ act contents)
(path:_) - >文件路径ReadMode(\handle-> do
contents< - hGetContents handle
(print(length $ lines contents)))
_ - > putStrLn参数错误
编辑2:谢谢
问题出现在 main
的第4行: opt1
和 opt2
是字符串,但是 argList
返回一个带整数对的列表。为了解决这个问题,你可以把 argList
更改为:
$ b
argList :: String - > [((String,String),String - > String)]
argList n =
let n1 = read n
in [((1,n),extrcta 1 n1)
,((2,n),extrcta 2 n1)
,((3,n),extrcta 3 n1)
]
$ c $现在它编译(但仍然没有按照预期工作;不明白你的算法应该做什么)。
更新对应于更新后的问题:这太复杂了。删除 argList
这个东西,并做一些简单的操作:
getRow: :Int - >字符串 - >字符串
getRow n =(!! n)。行
getField :: Int - >字符串 - >字符串
getField n =(!! n)。单词
getValue :: String - >字符串 - >字符串 - >字符串
getValue行字段src = getField(读取字段)$ getRow(读取行)src
main :: IO()
main = do
args< - getArgs
案例参数
(路径:opt1:opt2:_) - >做
src< - readFile路径
putStrLn $ getValue opt1 opt2 src
My code doesn't compile. It seems that the problem lies in the main function with lookup.
I am trying to parse an file that look like this :
auiauiaui@tsrntsr.epep 584756 "srtrtte"
jepeto@pinokio.disney 464456454 "cendrillon"
If you name the file lol and the code prog then calling prog lol 1 2 will return 464456454.
EDIT :
Thanks,
See I have a file named filetest like this :
field1-1 field1-2 field1-3
field2-1 field2-2 field2-3
field3-1 field3-2 field3-3
field4-1 field4-2 field4-3
field5-1 field5-2 field5-3
Now my piece of code is suppose to grab a field given the line and the rank.
mycode filetest give me the number of lines in the file (so that i can use a script for each lines).
Here some example :
mycode filetest 1 2 -> field1-2
mycode filetest 2 2 -> field2-2
mycode filetest 3 4 -> field3-4
mycode filetest 3 2 -> field3-2
mycode filetest 2 5 -> field2-5
mycode filetest 1 4 -> field1-4
Now each field contain a String.
What i do is grab the file as a string then grab the line wanted (with lines) and then grab the field (with words).
I corrected my errors (thanks to the #haskell and #haskell-fr channel and you). But my code doesn't work as expected at all.
Any help shall be appreciated. Here is the néw code :
import System.Environment
import System.IO
fieldArg :: Int -> [String] -> String
fieldArg x a = if length a > 2
then if x < 4 then a !! x else "field number is too high"
else "field number is too low"
lineArg :: Int -> [String] -> String
lineArg x a
| x > length a = "line number is too big"
| x < 1 = "line number is too low"
| otherwise = a !! x
extrcta :: Int -> Int -> String -> String
extrcta x n = fieldArg x . words . lineArg n . lines
argList :: String -> [((Int, String), String -> String)]
argList n = let n1 = read n
in [((1, n), extrcta 1 n1)
,((2, n), extrcta 2 n1)
,((3, n), extrcta 3 n1)
]
main :: IO ()
main = do
args <- getArgs
case args of
(path: opt1: opt2: _) -> case (read opt1, opt2) `lookup` argList opt2 of
Nothing -> putStrLn $ "Wrong argument " ++ opt1 ++ " or " ++ opt2
Just act -> withFile path ReadMode (\handle -> do
contents <- hGetContents handle
putStrLn $ act contents)
(path: _) -> withFile path ReadMode (\handle -> do
contents <- hGetContents handle
(print (length $ lines contents)))
_ -> putStrLn "Wrong number of argument"
EDIT2 : Thanks
解决方案 The problem is in the 4th line of main
: opt1
and opt2
are strings but argList
returns a list with integer pairs. To fix this you could change argList
to:
argList :: String -> [((String, String), String -> String)]
argList n =
let n1 = read n
in [(("1", n), extrcta 1 n1)
,(("2", n), extrcta 2 n1)
,(("3", n), extrcta 3 n1)
]
Now it compiles (but still does not seem to work as intended; don't understand what your algorithm is supposed to do).
UPDATE corresponding to the updated question: That's too complicated. Just drop that argList
thing and do something simple as:
getRow :: Int -> String -> String
getRow n = (!!n) . lines
getField :: Int -> String -> String
getField n = (!!n) . words
getValue :: String -> String -> String -> String
getValue row field src = getField (read field) $ getRow (read row) src
main :: IO ()
main = do
args <- getArgs
case args of
(path: opt1: opt2: _) -> do
src <- readFile path
putStrLn $ getValue opt1 opt2 src
这篇关于解析一个cv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!