-2

i have a working python script that checks the vat number of European companies. However checking 20 vat id by typing is not logical. I would like to check vat numbers from a file instead of manual typing every vat ids.

Here is the code:

from suds.client import Client
from urllib import getproxies
from pyfiglet import Figlet
import re 


custom_fig = Figlet(font='slant')
print(custom_fig.renderText('VATCheck'))

stringtosplit = raw_input('Enter vat number (like "EE102323452") :')
letters = ''.join(re.findall('([a-zA-Z])', stringtosplit))
numbers = ''.join(re.findall('([0-9])', stringtosplit))
VIES_URL = "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl"

client = Client(VIES_URL, proxy=getproxies())

response = client.service.checkVat(letters, numbers)

print (response)

How can i achieve to check several vat number from a vat_numbers.txt file?

elanozturk
  • 107
  • 4
  • 1
    Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – Tomerikoo Jan 13 '21 at 10:46

1 Answers1

1

Assuming your vat_numbers.txt file looks like:

EE102323452
EE102323453
EE102323454

Try the following:

from suds.client import Client
from urllib import getproxies
from pyfiglet import Figlet
import re 


custom_fig = Figlet(font='slant')
print(custom_fig.renderText('VATCheck'))

#stringtosplit = raw_input('Enter vat number (like "EE102323452") :')
with open('vat_numbers.txt', 'r') as handle:
    for stringtosplit in handle:
        letters = ''.join(re.findall('([a-zA-Z])', stringtosplit))
        numbers = ''.join(re.findall('([0-9])', stringtosplit))
        VIES_URL = "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl"

        client = Client(VIES_URL, proxy=getproxies())

        response = client.service.checkVat(letters, numbers)

        print (response)
anurag
  • 1,715
  • 1
  • 8
  • 28
  • Is there any way to add description lines in txt file like; EE102323452 # Company name ? – elanozturk Jan 13 '21 at 11:01
  • 1
    You can put each record (with as many fields as required) in each line of the text. Separate them with a separator, such as `,` or `;`. While reading back, add the following line after the for loop: `record = stringtosplit.splt(,)`. Now this record is a list, use indexing to access each field. – anurag Jan 13 '21 at 11:33
  • i get error; File "vat.py", line 15 record = stringtosplit.splt(,) ^ SyntaxError: invalid syntax – elanozturk Jan 13 '21 at 14:56
  • i put "record = stringtosplit.splt(,)" under; "numbers = ''.join(re.findall('([0-9])', stringtosplit))" – elanozturk Jan 13 '21 at 14:58
  • 1
    my bad, it was a typo, I meant: `record = stringtosplit.split(',')` (assuming the fields are separated by a `,` in your file). Place it right under the for loop. Then get each field using `record[i]` where `i` is index of the field. Then perform your desired processing. – anurag Jan 13 '21 at 15:36