Creating dictionaries from a text file - python

I have a text file which reads:
name,number,number,number
name,number,number,number
...
and I want to create a dictionary from it. I found this bit of code:
file = open('Results.txt', 'r')
lines = file.read().splitlines()
s = {lines[i]:[float(k) for k in lines[i+1:i+4]] for i in range(0,len(lines),4)}
However it only works for files with this format:
name
number
number
number
name
number
number
number
...
How can I make it work from my format? Also, is it possible to make it still work if there are less than 3 numbers after the name?

The following will handle lines with one or more comma-separated numbers following a name on each line of the file:
with open('Results.txt') as file:
s = {items[0]: [float(item) for item in items[1:]]
for items in (line.split(',') for line in file)}

Related

Proper way of inputting values from text file

I am having hard time to understand how to properly inert values into my variables from txt file. First line is number of test cases, then goes number of houses and then house binary string. Here are my input values:
2 (Number of tests [INT])
3 (Number of houses [INT])
111 (Binary string [String])
6 (Number of houses [INT])
100100 (Binary string [String])
I know we can do like this:
test_cases = int(input())
for i in range(test_cases):
house_number = int(input())
house_string = input()
some_function(int value1, string value2)
But I want to create txt file so I will not type these values every time. I know how to open and read txt file. However can not imagine how can I pass variables.
with open('test.txt') as file:
lines = file.readlines()
for line in lines:
...
As long as your text file is consistent with formatting you can loop over every two elements and turn them into a list of tuples. Note that this code excludes the first element assuming there are complete pairs:
with open('test.txt') as file:
output_lst = []
lines = file.readlines()
for i,k in zip(lines[1::2], lines[2::2]):
output_lst.append((int(i), str(k)))

Issues reading two text files and calculating values

I have two text files with numbers that I want to do some very easy calculations on (for now). I though I would go with Python. I have two file readers for the two text files:
with open('one.txt', 'r') as one:
one_txt = one.readline()
print(one_txt)
with open('two.txt', 'r') as two:
two_txt = two.readline()
print(two_txt)
Now to the fun (and for me hard) part. I would like to loop trough all the numbers in the second text file and then subtract it with the second number in the first text file.
I have done this (extended the coded above):
with open('two.txt') as two_txt:
for line in two_txt:
print line;
I don't know how to proceed now, because I think that the second text file would need to be converted to string in order do make some parsing so I get the numbers I want. The text file (two.txt) looks like this:
Start,End
2432009028,2432009184,
2432065385,2432066027,
2432115011,2432115211,
2432165329,2432165433,
2432216134,2432216289,
2432266528,2432266667,
I want to loop trough this, ignore the Start,End (first line) and then once it loops only pick the first values before each comma, the result would be:
2432009028
2432065385
2432115011
2432165329
2432216134
2432266528
Which I would then subtract with the second value in one.txt (contains numbers only and no Strings what so ever) and print the result.
There are many ways to do string operations and I feel lost, for instance I don't know if the methods to read everything to memory are good or not.
Any examples on how to solve this problem would be very appreciated (I am open to different solutions)!
Edit: Forgot to point out, one.txt has values without any comma, like this:
102582
205335
350365
133565
Something like this
with open('one.txt', 'r') as one, open('two.txt', 'r') as two:
next(two) # skip first line in two.txt
for line_one, line_two in zip(one, two):
one_a = int(split(line_one, ",")[0])
two_b = int(split(line_two, " ")[1])
print(one_a - two_b)
Try this:
onearray = []
file = open("one.txt", "r")
for line in file:
onearray.append(int(line.replace("\n", "")))
file.close()
twoarray = []
file = open("two.txt", "r")
for line in file:
if line != "Start,End\n":
twoarray.append(int(line.split(",")[0]))
file.close()
for i in range(0, len(onearray)):
print(twoarray[i] - onearray[i])
It should do the job!

While trying to copy data Next to variables from text to csv with python getting error:

I am trying to copy values of data seprated with: from text file.
Text file having data like in this form:
I have 50+ text file contains data in this form:
Type: Assume
Number: 123456
Name: Assume
Phone Number: 000-000
Email Address: any#gmail.com
Mailing Address: Assume
i am trying to get data values in this format in csv from multiple text files:
Type Number Name Phone email Mailing Address
Assume 123456 Assume 000-000 any#gmail.com Assume
Here is the code:
import re
import csv
file_h = open("out.csv","a")
csv_writer = csv.writer(file_h)
def writeHeading(file_content):
list_of_headings = []
for row in file_content:
key = str(row.split(":")[0]).strip()
list_of_headings.append(key)
csv_writer.writerow(tuple(list_of_headings))
def writeContents(file_content):
list_of_data = ['Number']
for row in file_content:
value = str(row.split(":")[1]).strip()
list_of_data.append(value)
csv_writer.writerow(tuple(list_of_data))
def convert_txt_csv(filename):
file_content = open(filename,"r").readlines()
return file_content
list_of_files = ["10002.txt","10003.txt","10004.txt"]
# for writing heading once
file_content = convert_txt_csv(list_of_files[0])
writeHeading(file_content)
# for writing contents
for file in list_of_files:
file_content = convert_txt_csv(file)
writeContents(file_content)
file_h.close()
Here is the following error:
Traceback (most recent call last):
File "Magnet.py", line 37, in <module>
writeContents(file_content)
File "Magnet.py", line 20, in writeContents
value = str(row.split(":")[1]).strip()
IndexError: list index out of range
Your code probably encounters a blank line at the end of the first file, or any line that doesn't have a : in it, so when you try to split it into key/values it complains as it didn't get a list of expected length. You can fix that easily by checking if there is a colon on the current line, i.e.:
for row in file_content:
if ":" not in row: # or you can do the split and check len() of the result
continue
key = row.split(":")[0].strip()
list_of_headings.append(key)
But... While the task you're attempting looks extremely simple, keep in mind that your approach assumes that all the files are equal, with equal number key: value combinations and in the same order.
You'd be much better off by storing your parsed data in a dict and then using csv.DictWriter() to do your bidding.

Same value in list keeps getting repeated when writing to text file

I'm a total noob to Python and need some help with my code.
The code is meant to take Input.txt [http://pastebin.com/bMdjrqFE], split it into seperate Pokemon (in a list), and then split that into seperate values which I use to reformat the data and write it to Output.txt.
However, when I run the program, only the last Pokemon gets outputted, 386 times. [http://pastebin.com/wkHzvvgE]
Here's my code:
f = open("Input.txt", "r")#opens the file (input.txt)
nf = open("Output.txt", "w")#opens the file (output.txt)
pokeData = []
for line in f:
#print "%r" % line
pokeData.append(line)
num = 0
tab = """ """
newl = """NEWL
"""
slash = "/"
while num != 386:
current = pokeData
current.append(line)
print current[num]
for tab in current:
words = tab.split()
print words
for newl in words:
nf.write('%s:{num:%s,species:"%s",types:["%s","%s"],baseStats:{hp:%s,atk:%s,def:%s,spa:%s,spd:%s,spe:%s},abilities:{0:"%s"},{1:"%s"},heightm:%s,weightkg:%s,color:"Who cares",eggGroups:["%s"],["%s"]},\n' % (str(words[2]).lower(),str(words[1]),str(words[2]),str(words[3]),str(words[4]),str(words[5]),str(words[6]),str(words[7]),str(words[8]),str(words[9]),str(words[10]),str(words[12]).replace("_"," "),str(words[12]),str(words[14]),str(words[15]),str(words[16]),str(words[16])))
num = num + 1
nf.close()
f.close()
There are quite a few problems with your program starting with the file reading.
To read the lines of a file to an array you can use file.readlines().
So instead of
f = open("Input.txt", "r")#opens the file (input.txt)
pokeData = []
for line in f:
#print "%r" % line
pokeData.append(line)
You can just do this
pokeData = open("Input.txt", "r").readlines() # This will return each line within an array.
Next you are misunderstanding the uses of for and while.
A for loop in python is designed to iterate through an array or list as shown below. I don't know what you were trying to do by for newl in words, a for loop will create a new variable and then iterate through an array setting the value of this new variable. Refer below.
array = ["one", "two", "three"]
for i in array: # i is created
print (i)
The output will be:
one
two
three
So to fix alot of this code you can replace the whole while loop with something like this.
(The code below is assuming your input file has been formatted such that all the words are split by tabs)
for line in pokeData:
words = line.split (tab) # Split the line by tabs
nf.write ('your very long and complicated string')
Other helpers
The formatted string that you write to the output file looks very similar to the JSON format. There is a builtin python module called json that can convert a native python dict type to a json string. This will probably make things alot easier for you but either way works.
Hope this helps

Adding words from a text file to a list (python 2.7)

Assuming I have a text file.
My goal is to write a function which receives a number of line to go over in the text file and returns a list, each cell in the list containing one word exactly from that line.
Any idea of how doing this ?
thanks
If you are working with small files:
def get_words(mifile, my_line_number):
with open(mifile) as f:
lines = f.readlines()
myline = lines[my_line_number] #first line is 0
return myline.split()
you get all the file lines in the list lines. This is not very efficient for VERY big files. In that case probably it would be better to iterate line by line until you arrive to the chosen line.
Given the filename and the line number (lineno), you could extract the words on that line this way:
Assuming the lineno is not too large:
import linecache
line = linecache.getline(filename, lineno)
words = line.split()
Or, if the lineno is large:
import itertools
with open(filename,'r') as f:
line = next(itertools.islice(f,lineno-1,None))
words = line.split()
This,of course,assumes that words are separated by spaces--which may not be the case in hard-to-parse text.

Categories