Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 2nd, 2012, 09:51 PM
pullingMyHair pullingMyHair is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 6 pullingMyHair User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 47 sec
Reputation Power: 0
Confusion on txt files, lists and for loops

I have a text file with the following credit card numbers (not real):

Code:
4388576018402626
4388576018410707
4012888888881881
4552720412345677
4539992043491562
4992739871600
4992739870017
80840123456789
5588320123456789
5491946915444923
5490123456789128
378282246310005
371449635398431
371449635398431
378734493671000
378734493671001
6041273990139424
6011111111111117
6011000990139424


I made a program that opens the file, reads the first line of text, then checks to see if it is a valid number. Here is the code for that:

Code:
def readfile():
  '''Read a text file; return a string holding the text'''
  f = open("numbers.txt", 'r')
  text = f.readline()
  f.close()
  return text

def dataConversion(text):

    # create a list called lst and use getLine as input for the list
    lst = text.strip()
       
    # convert strings into ints
    lst = map(int, lst)
       
    #return lst to main() function
    return lst

def evenNumberList(lst):

    #strip last digit from lst
    lst.pop()
    
    # make a new list with every other number from right to left
    evenNumber =[lst[i] for i in range(len(lst)-1,-1,-2)]
    
    # create empty list
    doubledList = []
    
    # loop through the list
    for num in evenNumber:
        
        #double each number in the list
        value = num *2
        
        #append new value to doubledList list 
        doubledList.append(value)
    
    return doubledList


def dataAddition(doubledList):
          
    # make new list consisting of the single digit numbers of the
    # doubledList
    listOfSingleDigits = [x for x in doubledList if x <= 9]
    
    # make new list consisting of the double digit numbers of the
    # doubledList.  Split the double digits into single numbers
    # and add them together to get single digits.
    listOfDoubleDigits = [x % 10 + x / 10 for x in doubledList if x > 9]
    
    # create add fuction for reduce statement
    def add(x, y): return x + y
    # add the values of the listOfSingleDigits list
    sumOfList1 = reduce(add, listOfSingleDigits)
    
    # add the values of the listOfDoubleDigits list
    sumOfList2 = reduce(add, listOfDoubleDigits)
    
    #add sumOfList1, sumOfList2, sumOfList3
    sumOfAllSingleDigits = sumOfList1 + sumOfList2 
            
    return sumOfAllSingleDigits

def oddNumberList(lst):
    
    # make a new list with every other number from right to left
    oddNumber =[lst[i] for i in range(len(lst)-1,-1,-2)]
    
    #create add function for reduce statement  
    def add(x, y): return x + y

    sumOfOddNumbers = reduce(add, oddNumber)
    
    return sumOfOddNumbers

def checkCardValidity(sumOfAllSingleDigits, sumOfOddNumbers, text):

    # read one line of text from the text file 
    creditCardNumber = text    

    #add numbers for validation 
    sumOfEvenAndOdd = sumOfAllSingleDigits + sumOfOddNumbers
    
    #create if statement to check card validity
    if sumOfEvenAndOdd / 10 == 0:
        print creditCardNumber, "valid"  
    else:
        print creditCardNumber, "invalid" 
   

   
def main():

    text = readfile()    
    lst = dataConversion(text)
    doubledList = evenNumberList(lst)
    sumOfAllSingleDigits = dataAddition(doubledList)
    sumOfOdd = oddNumberList(lst)
    sumOfOddNumbers = oddNumberList(lst)
    checkCardValidity(sumOfAllSingleDigits, sumOfOddNumbers, text)


main()


Now that I have that working, I am trying to go back and get it to loop through each line of numbers. Here is where I am having problems. Or I think I am. I assumed that when the text file went through the loop it would end up looking like this:

Code:

['4388576018402626']
['4388576018410707']
['4012888888881881']
['4552720412345677']
['4539992043491562']
['4992739871600']
['4992739870017']
['80840123456789']
['5588320123456789']
['5491946915444923']
['5490123456789128']
['378282246310005']
['371449635398431']
['371449635398431']
['378734493671000']
['378734493671001']
['6041273990139424']
['6011111111111117']
['6011000990139424']


Then from there I could split each large string up into individual character strings within the list. However, it's not working out as I planned. Instead I get the following:

Code:

['4', '3', '8', '8', '5', '7', '6', '0', '1', '8', '4', '0', '2', '6', '2', '6', '', '4', '3', '8', '8', '5', '7', '6', '0', '1', '8', '4', '1', '0', '7', '0', '7', '', '4', '0', '1', '2', '8', '8', '8', '8', '8', '8', '8', '8', '1', '8', '8', '1', '', '4', '5', '5', '2', '7', '2', '0', '4', '1', '2', '3', '4', '5', '6', '7', '7', '', '4', '5', '3', '9', '9', '9', '2', '0', '4', '3', '4', '9', '1', '5', '6', '2', '', '4', '9', '9', '2', '7', '3', '9', '8', '7', '1', '6', '0', '0', '', '4', '9', '9', '2', '7', '3', '9', '8', '7', '0', '0', '1', '7', '', '8', '0', '8', '4', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '', '5', '5', '8', '8', '3', '2', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '', '5', '4', '9', '1', '9', '4', '6', '9', '1', '5', '4', '4', '4', '9', '2', '3', '', '5', '4', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '2', '8', '', '3', '7', '8', '2', '8', '2', '2', '4', '6', '3', '1', '0', '0', '0', '5', '', '3', '7', '1', '4', '4', '9', '6', '3', '5', '3', '9', '8', '4', '3', '1', '', '3', '7', '1', '4', '4', '9', '6', '3', '5', '3', '9', '8', '4', '3', '1', '', '3', '7', '8', '7', '3', '4', '4', '9', '3', '6', '7', '1', '0', '0', '0', '', '3', '7', '8', '7', '3', '4', '4', '9', '3', '6', '7', '1', '0', '0', '1', '', '6', '0', '4', '1', '2', '7', '3', '9', '9', '0', '1', '3', '9', '4', '2', '4', '', '6', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '7', '', '6', '0', '1', '1', '0', '0', '0', '9', '9', '0', '1', '3', '9', '4', '2', '4', '']


It's just one giant list of individual character strings.

How do I get each number into it's own list so that manipulate each list and make it work with the rest of my program like the single number did?

Here is what I tried:

Code:

def dataConversion(text):

    lst = []
    for line in text:
        numberList = line.strip()
        lst.append(numberList)
    print lst


I tried to go beyond that by converting the single character strings into int data types but it it gives me the following error message: ValueError: invalid literal for int() with base 10: ''

Reply With Quote
  #2  
Old February 3rd, 2012, 11:10 AM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 102 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 12 h 39 m 48 sec
Reputation Power: 1
Ssss

Briefly glancing at your code, it appears that in line 4 you need

text = f.readlines() i.e. plural of readline

Reply With Quote
  #3  
Old February 3rd, 2012, 01:35 PM
pullingMyHair pullingMyHair is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 6 pullingMyHair User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 47 sec
Reputation Power: 0
Yes, thank you. That helped.

Reply With Quote
  #4  
Old February 3rd, 2012, 03:51 PM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 102 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 12 h 39 m 48 sec
Reputation Power: 1
Good

Good. Good luck.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Confusion on txt files, lists and for loops


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap