Skip to content

Instantly share code, notes, and snippets.

@Fi3
Last active August 2, 2016 10:10
Show Gist options
  • Save Fi3/f750f50ed36e762d739e0a4a3be80417 to your computer and use it in GitHub Desktop.
Save Fi3/f750f50ed36e762d739e0a4a3be80417 to your computer and use it in GitHub Desktop.
#-----------------------tipo1-----------------------#
import csv
parsed = []
nomeFile = 'eur1.csv'
with open(nomeFile,'r') as csvfile:
rd = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in rd:
parsed.append(row)
def findRef(string):
firstPosition = string.find('VS RIF: ')
if firstPosition == -1:
return 'NON DEF'
secondPosition = string[firstPosition+8:-1].find(' ')
return string[firstPosition+8:-1][0:secondPosition]
def findIsin(string):
firstPosition = string.find('ULTERIORI RIF: ')
if firstPosition == -1:
return 'NON DEF'
secondPosition = string[firstPosition+15:-1].find(' ')
return string[firstPosition+15:-1][0:secondPosition].replace('.','')
for row in parsed:
string = row[4]
ref = findRef(string)
isin = findIsin(string)
row.append(ref)
row.append(isin)
with open('gg.csv', 'w') as csvfile:
wrt = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in parsed:
wrt.writerow(row)
#-----------------------tipo2-----------------------#
import csv
parsed = []
nomeFile = 'input.csv'
with open(nomeFile,'r') as csvfile:
rd = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in rd:
parsed.append(row)
#string: string where is the information that I need
#stringBefore: substring of string that come sooner before the information that I need
#stringAfter: substring of string that come sooner sfter the information that I need
def findQuantity(string, stringBefore, stringAfter):
firstPosition = string.find(stringBefore)
if firstPosition == -1:
return 'NON DEF'
secondPosition = string[firstPosition+len(stringBefore):-1].find(stringAfter)
return string[firstPosition+len(stringBefore):-1][0:secondPosition].replace('.', '').replace('-', '.')
for row in parsed:
string = row[4]
stringsBefore = ['NR. ', 'VS ACQUISTO ', 'VS VENDITA ', 'C.N. ', 'FMT ']
stringAfter = ' '
qty = ''
for stringBefore in stringsBefore:
qty = findQuantity(string, stringBefore, stringAfter)
if qty != 'NON DEF':
break
try:
qty = float(qty)
except:
qty = 'NON DEF'
print(qty)
row.append(qty)
with open('output.csv', 'w') as csvfile:
wrt = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in parsed:
wrt.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment