Extract Date and Currency value(separated by comma) from file

雨燕双飞 提交于 2020-01-23 17:21:26

问题


Objective:

Extract String data, Currency value , [type of currency] and date.

Content of file:

[["1234567890","Your previous month subscription point is <RS|$|QR|#> 5,200.33.Your current month month subscription point is <RS|$|QR|#> 1,15,200.33, Last Year total point earned <RS|$|QR|#> 5589965.26 and point lost in game is <RS|$|QR|#> 11520 your this year subscription will expire on 19-04-2013. 9. Back"],["1234567890","Your previous month subscription point is <RS|$|QR|#> 5,200.33.Your current month month subscription point is <RS|$|QR|#> 1,15,200.33, Last Year total point earned <RS|$|QR|#> 5589965.26 and point lost in game is <RS|$|QR|#> 11520 your this year subscription will expire on 19-04-2013. 9. Back"]]

What I have done so far:

def read_file():
        fp = open('D:\\ReadData2.txt', 'rb')
        content = fp.read()
        data = eval(content)  
        l1 = ["%s" % x[1] for x in data]
        return l1

    def check_currency(l2):
        import re
        for i in range(l2.__len__()):
            newstr2  = l2[i]
            val_currency = []
            val_currency.extend(re.findall(r'([+-]?\d+(?:\,\d+)*?\d+(?:\.\d+)?)',newstr2))
            print " List %s " %  val_currency
            for i in range(len(val_currency)):
                val2 =  val_currency[i]
                remove_commas = re.compile(r',(?=\d+)*?')
                val3 = remove_commas.sub('', val2)
                print val3              

     if __name__=="__main__":main()

EDIT UDP I am able to extract the currency value but with the currency of -ve value are conflicting with date format(dd-mm-yyyy). And during extracting string value its also extracting [.|,|] how not to read these characters.

Ouput of check_currency:

>List ['5,200.33', '1,15,200.33', '5589965.26', '11520', '19', '-04', '-2013'] 
>5200.33
>115200.33
>5589965.26
>11520
>19
>-04
>-2013

Expected Ouput of check_currency:

>List ['5,200.33', '1,15,200.33', '5589965.26', '11520'] 
        >5200.33
        >115200.33
        >5589965.26
        >11520

回答1:


I added this <RS|$|QR|#>\s* at the first part of your regular expression so as to be used as prefix for the currency value you want to match.

You can change your code to this one:

def check_currency(l2):
import re
for i in range(l2.__len__()):
    newstr2  = l2[i]
    val_currency = []
    val_currency.extend(re.findall(r'<RS|$|QR|#>\s*([+-]?\d+(?:\,\d+)*?\d+(?:\.\d+)?)',newstr2))
    # skip empty strings and remove comma characters
    val_currency = [v.replace(',', '') for v in val_currency if v]
    print " List %s " %  val_currency$                                                            
    for i in range(len(val_currency)):
        val2 =  val_currency[i]
        remove_commas = re.compile(r',(?=\d+)*?')
        val3 = remove_commas.sub('', val2)
        print val3

Output:

List ['5200.33', '115200.33', '5589965.26', '11520']
5200.33
115200.33
5589965.26
11520

aditions in the code:

val_currency.extend(re.findall(r'<RS|$|QR|#>\s*([+-]?\d+(?:\,\d+)*?\d+(?:\.\d+)?)',newstr2))
val_currency = [v.replace(',', '') for v in val_currency if v]


来源:https://stackoverflow.com/questions/15923158/extract-date-and-currency-valueseparated-by-comma-from-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!