I have recently began working with text files in python (how to return them in different ways, how to produce a text file and so on) and I discovered that a text file can also be returned as a dicitonary in python and I was wondering how one could achieve that. Let's say i have a text file including some types of currencies and their buy and sell values and i wanted to return them in some sort of a dictionary. The text file would for example include:
Num Date____ Bank Type_______ NCu CCu Buy___________ Sell__________
001 20161130 NLB_ individuals 840 USD 0001,074800000 0001,050800000
and would return some sort of dictionary, that would include the currency as the keys and a tuple of its buy and sell values as the key. So the upper text file would produce:
'USD': (1.0748, 1.0508)}
Help on how to achieve this sort of return in python would be greatly appreciated.
-
You could read the txt file as a csv delimited by space/tab, using csv DictReader docs.python.org/2/library/csv.html#csv.DictReaderSushant– Sushant2016年12月02日 15:35:35 +00:00Commented Dec 2, 2016 at 15:35
2 Answers 2
Let's process the file line per line, create a dictionary, split the line, and create entries, converting text to floats in the process (commas have to be replaced by dots)
test="""Num Date____ Bank Type_______ NCu CCu Buy___________ Sell__________
001 20161130 NLB_ individuals 840 USD 0001,074800000 0001,050800000
001 20161130 NLB_ individuals 840 CHF 0002,074800000 0004,050800000
""".splitlines().__iter__()
d = dict()
next(test) # skip title line
for line in test:
toks = line.split()
if len(toks)==8:
d[toks[5]] = (float(toks[6].replace(",",".")),float(toks[7].replace(",",".")))
print(d)
result:
{'USD': (1.0748, 1.0508), 'CHF': (2.0748, 4.0508)}
That's just for a standalone test. When running with a file, just replace the string by:
with open("input.txt") as test:
and it will act the same, but with a file as input.
(or use the csv module but 1) that seems to be overkill for your case, and 2) we don't know if the separators are tabs, space, or more than 1 space/tab. str.split handles that too so I'd stick with it until fields containing spaces appear :))
Comments
text:
Num Date____ Bank Type_______ NCu CCu Buy___________ Sell__________
001 20161130 NLB_ individuals 840 USD 0001,074800000 0001,050800000
main.py:
exchange_rates = {}
with open("text") as file:
next(file)
for line in file:
data = line.split(" ")
ccu = data[5]
buy = float(data[6].replace(",", "."))
sell = float(data[7].replace(",", "."))
exchange_rates[ccu] = (buy, sell)
print(exchange_rates)
You split each line at the spaces " " into a list containing the data:
data = line.split(" ")
Then you have to convert the string "0001,07400" into a float. One can use float( ) for this, but this needs that "0001,07400" is transformed into "0001.0740" first:
float(data[6].replace(",", "."))
This is simple file parsing. You have to be cautious though, when data is missing (ie 7 instead of 8 entries) or is formatted differently (0001.074 instead of 0001,074) or or or... For such cases you might have to make extra distinctions.