I have a text file that has three lines and would like the first number of each line stored in an array, the second in another, so on and so fourth. And for it to print the array out.
The text file:
0,1,2,3,0
1,3,0,0,2
2,0,3,0,1
The code I'm using (I've only showed the first array for simplicity):
f=open("ballot.txt","r")
for line in f:
num1=line[0]
num1=[]
print(num1)
I expect the result for it to print out the first number of each line:
0
1
2
the actual result i get is
[]
[]
[]
It looks like you reset num1 right? Every time num1 is reseted to an empty list before printing it.
f=open("ballot.txt","r")
for line in f:
num1=line[0]
#num1=[] <-- remove this line
print(num1)
This will return the first char of the line. If you want the first number (i.e. everything before the first coma), you can try this:
f=open("ballot.txt","r")
for line in f:
num1=line.split(',')[0]
print(num1)
You read in the line fine and assign the first char of the line to the variable, but then you overwrite the variable with an empty list.
f=open("ballot.txt","r")
for line in f:
num1=line.strip().split(',')[0] # splits the line by commas and grabs 1st val
# ^^^^^^^^^^^^^^^^^^^^^^^^^^
print(num1)
This should do what you want. In your simple case, it's index 0, but you could index any value.
Since the file is comma-delimited, splitting the line by the comma will give you all the columns. Then you index the one you want. The strip() call gets rid of the newline character (which would otherwise be hanging off the last column value).
As for the big picture, trying to get lists from each column, read in the whole file into a data structure. Then process the data structure to make your lists.
def get_column_data(data, index):
return [values[index] for values in data]
with open("ballot.txt", "r") as f:
data = f.read()
data_struct = []
for line in data.splitlines():
values = line.split(',')
data_struct.append(values)
print(data, '\nData Struct is ', data_struct)
print(get_column_data(data_struct, 0))
print(get_column_data(data_struct, 1))
The get_column_data function parses the data structure and makes a list (via list comprehension) of the values of the proper column.
In the end, the data_struct is a list of lists, which can be accessed as a two-dimensional array if you wanted to do that.
Related
I have a .txt file that was saved with python. It has the form:
file_inputs
Where the first line is just a title that helps me remember the order of each element that was saved and the second line is a sequence of a string ('eos') and other elements inside. How can I call the elements so that inputs[0] returns a string ('eos') and inputs[1] returns the number "5", for example?
I am not sure why you want inputs[&] to return 5.
However here is a the standard (simple) way to read a text file with python:
f = open('/path/to/file.txt', 'r')
content = f. read()
#do whatever you want there
f. close()
To get eos printed first you might want to iterate through the content string until you find a space.
For the 5 idk.
if i could understand, you will have to do something like this
input = open(<file_name>, 'r')
input = input.readlines()
input.pop(0) #to remove the title str
#now you can have an array in wich line of .txt file is a str
new_input = [None]*len(input)
for index, line in enumerate(input):
new_input[index] = line.split(",") #with this your input should be an array of arrays in wich element is a line of your .txt with all your elements
#in the end you should be able to call
input[0][1] #first line second element if i didnt mess up this should be 5
I'm trying to use python to open and read a file with a line that repeats in my output. The line is:
"AVE. CELL LNTHS[bohr] = 0.4938371E+02 0.4938371E+02 0.4938371E+02"
the values change in each line ( with every step), but all lines start with AVE. CELL LNTHS[bohr]. I want to take the first of the three values from every line, and make a list.the image is a snip of the output file and repeating line of interest.
You can use the float command to convert a string to number. Also, use split to split the line first on the '=' then on space. Lastly, use list comprehension to build a list from the parts of the string.
path_to_file = r"C:\Documents\whatever.csv"
with open(path_to_file, "r") as file:
for line in file:
if line.startswith("AVE. CELL LNTHS[bohr]"):
values = [float(x) for x in line.split("=")[1].split()]
# Do something with the values
print(values)
I have a file containing:
name: Sam
placing: 2
quote: I'll win.
name: Jamie
placing: 1
quote: Be the best.
and I want to read the file through python and append specific contents into a list. I want my first list to contain:
rank = [['Sam', 2],['Jamie', 1]]
and second list to contain:
quo = ['I'll win','Be the best']
first off, i start reading the file by:
def read_file():
filename = open("player.txt","r")
playerFile = filename
player = [] #first list
quo = [] #second list
for line in playerFile: #going through each line
line = line.strip().split(':') #strip new line
print(line) #checking purpose
player.append(line[1]) #index out of range
player.append(line[2])
quo.append(line[3])
I'm getting an index out of range in the first append. I have split by ':' but I can't seem to access it.
When you do line = line.strip().split(':') when line = "name: Sam"
you will receive ['name', ' Sam'] so first append should work.
The second one player.append(line[2] will not work.
As zython said in the comments , you need to know the format of the file and each blank line or other changes in the file , can make you script to fail.
You should analyze the file differently:
If you can rely on the fact that "name" and "quote" are always existing fields in each player data , you should look for this field names.
for example:
for line in file:
# Run on each line and insert to player list only the lines with "name" in it
if ("name" in line):
# Line with "name" was found - do what you need with it
player.append(line.split(":")[1])
A few problems,
The program attempts to read three lines worth of data in a single iteration of the for loop. But that won't work, because the loop, and the split command are parsing only a single line per iteration. It will take three loop iterations to read a single entry from your file.
The program needs handling for blank lines. Generally, when reading files like this, you probably want a lot of error handling, the data is usually not formatted perfectly. My suggestion is to check for blank lines, where line has only a single value which is an empty string. When you see that, ignore the line.
The program needs to collect the first and second lines of each entry, and put those into a temporary array, then append the temporary array to player. So you'll need to declare that temporary array above, populate first with the name field, next with the placing field, and finally append it to player.
Zero-based indexing. Remember that the first item of an array is list[0], not list[1]
I think you are confused on how to check for a line and add content from line to two lists based on what it contains. You could use in to check what line you are on currently. This works assuming your text file is same as given in question.
rank, quo = [], []
for line in playerFile:
splitted = line.split(": ")
if "name" in line:
name = splitted[1]
elif "placing" in line:
rank.append([name, splitted[1]])
elif "quote" in line:
quo.append(splitted[1])
print(rank) # [['Sam', '2'],['Jamie', '1']]
print(quo) # ["I'll win",'Be the best']
Try this code:
def read_file():
filename = open("player.txt", "r")
playerFile = filename
player = []
rank = []
quo = []
for line in playerFile:
value = line.strip().split(": ")
if "name" in line:
player.append(value[1])
if "placing" in line:
player.append(value[1])
if "quote" in line:
quo.append(value[1])
rank.append(player)
player = []
print(rank)
print(quo)
read_file()
I'm trying take a .txt file populated by 88 rows, each of which has two characters separated by a space, copy the first character in each row into a list #1, copy the second character of each list into a list #2 and then populate a dictionary with those two lists. Something, however, is going wrong when I try to copy down the data from the file into my lists. Could you tell me what I'm not doing correctly?
I keep getting this error: "IndexError: string index out of range" at the line where I have typed "column1[count] = readit[0]"
def main():
modo = open('codes.txt', 'r') #opening file
filezise = 0 #init'ing counter
for line in modo:
filezise+=1 #counting lines in the file
column1 = []*filezise
column2 = []*filezise #making the lists as large as the file
count = 0 #init'ing next counter
while count < filezise+1:
readit = str(modo.readline())
column1[count] = readit[0] ##looping through the file and
column2[count] = readit[2] ##populating the first list with the
count+=1 #first character and the second list
print(column1, column2) #with the second character
index = 0
n = 0
codebook = {} #init'ing dictionary
for index, n in enumerate(column1): #looping through to bind the key
codebook[n] = column2[index] #to its concordant value
print(codebook)
main()
When you write
for line in modo:
filezise+=1
You have already consumed the file.
If you want to consume it again, you need to do modo.seek(0) first to rewind the file back.
If you do not rewind the file, the line below will return an empty string, because there is nothing left in the file.
readit = str(modo.readline())
Of course, there's no real need to go through the file twice. You can just do it once and append to your lists.
column1 = []
column2 = []
for line in modo:
filezise+=1
column1.append(line[0])
column2.append(line[2])
Try this
codebook = dict([''.join(line.strip().split(' ')) for line in open('codes.txt').readlines()])
You are getting the error because column1 = []*filezise doesn't actually make a list of length filezise. (If you look at the result, you will see that column1 is just an empty list.) When you try to access column1[count] when count > 0, you will get that error because there is nothing in column1 with an index greater than 0.
You shouldn't be trying to initialize the list. Instead, iterate over the lines in the file and append the appropriate characters:
column1=[]
column2=[]
for line in file('codes.txt'):
column1.append(line[0])
column2.append(line[2])
There's a much simpler way to get a dictionary from your file, by using the csv module and the dict() built-in function:
import csv
with open('codes.txt', 'rb') as csvfile:
codebook = dict(csv.reader(csvfile, delimiter=' '))
So long as the intermediate lists aren't being used, you could also use a dictionary comprehension to do everything in one go.
with open('codes.txt', 'r') as f:
codebook = {line[0]: line[-1] for line in f.read().splitlines()}
I'm trying take a .txt file populated by 88 rows, each of which has two characters separated by a space, copy the first character in each row into a list #1, copy the second character of each list into a list #2 and then populate a dictionary with those two lists. Something, however, is going wrong when I try to copy down the data from the file into my lists. Could you tell me what I'm not doing correctly?
I keep getting this error: "IndexError: string index out of range" at the line where I have typed "column1[count] = readit[0]"
def main():
modo = open('codes.txt', 'r') #opening file
filezise = 0 #init'ing counter
for line in modo:
filezise+=1 #counting lines in the file
column1 = []*filezise
column2 = []*filezise #making the lists as large as the file
count = 0 #init'ing next counter
while count < filezise+1:
readit = str(modo.readline())
column1[count] = readit[0] ##looping through the file and
column2[count] = readit[2] ##populating the first list with the
count+=1 #first character and the second list
print(column1, column2) #with the second character
index = 0
n = 0
codebook = {} #init'ing dictionary
for index, n in enumerate(column1): #looping through to bind the key
codebook[n] = column2[index] #to its concordant value
print(codebook)
main()
When you write
for line in modo:
filezise+=1
You have already consumed the file.
If you want to consume it again, you need to do modo.seek(0) first to rewind the file back.
If you do not rewind the file, the line below will return an empty string, because there is nothing left in the file.
readit = str(modo.readline())
Of course, there's no real need to go through the file twice. You can just do it once and append to your lists.
column1 = []
column2 = []
for line in modo:
filezise+=1
column1.append(line[0])
column2.append(line[2])
Try this
codebook = dict([''.join(line.strip().split(' ')) for line in open('codes.txt').readlines()])
You are getting the error because column1 = []*filezise doesn't actually make a list of length filezise. (If you look at the result, you will see that column1 is just an empty list.) When you try to access column1[count] when count > 0, you will get that error because there is nothing in column1 with an index greater than 0.
You shouldn't be trying to initialize the list. Instead, iterate over the lines in the file and append the appropriate characters:
column1=[]
column2=[]
for line in file('codes.txt'):
column1.append(line[0])
column2.append(line[2])
There's a much simpler way to get a dictionary from your file, by using the csv module and the dict() built-in function:
import csv
with open('codes.txt', 'rb') as csvfile:
codebook = dict(csv.reader(csvfile, delimiter=' '))
So long as the intermediate lists aren't being used, you could also use a dictionary comprehension to do everything in one go.
with open('codes.txt', 'r') as f:
codebook = {line[0]: line[-1] for line in f.read().splitlines()}