Printing elements and ignoring last two element in a table - python

I have a text file containing:
SKT:SSG:2:1
LN:FNC:1:1
NWS:ENY:2:0
I want to print out the elements in a table ignoring the last two elements which are the digits. Here's what I've done so far:
fileName = input("Enter file name:")
match = open(fileName)
table = []
for line in match:
contents = line.strip().split(':')
table.append(contents)
#add a loop to leave out last two digits?
print(table)
The output I get for a start is:
[['SKT','SSG','2','1'],['LN','FNC','1',1'],['NWS','ENY','2','0']]
The output I want:
[['SKT','SSG'],['LN','FNC'],['NWS','ENY']]
I've looked into a term known as array slicing and managed to come up with:
for i in range(len(table)):
print(table[i][:-2])
but I do not know how to implement it into the code I've written as I'm not familiar with file reading just yet. Any help is appreciated. Regards.

Assuming the full row is needed in table for another usage, you can create a new list and print the new one:
print([row[:-2] for row in table])
If the full row is not required, you can add just the relevant values, as CoryKramer mentioned in the question comments.

You can simply modify your current code as below:
contents = line.strip().split(':')[:-2]
Keep the remaining code as it is. Split method returns a list, so you can do slicing on it as well.

This code reads the file line by line. Each line is split on the ':' character to form a list and the list is appended to the table list to form a list of lists. Finally the each list in table is printed minus its last two elements.
fileName = input("Enter file name:")
match = open(fileName)
table = []
for line in match:
contents = line.strip().split(':')
table.append(contents)
print([content[:-2] for content in table])

Related

Reading and taking specific file contents in a list in python

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()

Writing to a text file error - Must be str, not list

I have been having problems with some code I am writing. Basically, when I run the code I enter an 8 digit number and it should scan the CSV file to see if the number is inside the file. If it is, the row should be written to the text file. However, when I run it and I enter a number, I get this:
TypeError: must be str, not list
And even when it is fixed, the output is:
<_io.TextIOWrapper name='receipt.txt' mode='a' encoding='cp1252'>
My code is as follows:
import csv
import sys
import re
addItem = ""
gtinNum = ""
quantity = 0
totalPrice = 0
receipt = open("receipt.txt", "r+")
f = open("ChocolateCSV.csv", "rt")
def scanGTIN():
rows = re.split('\n', f.read())
for index, row in enumerate(rows):
global cells
cells = row.split(',')
if gtinNum in cells:
receipt.write(cells)
def gtinQuestion():
global gtinNum
gtinNum = input("Please enter the GTIN-8 Code of the product you would like to order:")
if gtinNum.isdigit() == False or len(gtinNum) != 8:
gtinQuestion()
elif gtinNum.isdigit() == True and len(gtinNum) == 8:
scanGTIN()
gtinQuestion()
The write method of a Python text file takes a string (str), not a list.
If you want to duplicate matching rows from the input file to the output, as your description implies, I think you want to either replace your
receipt.write(cells)
with
receipt.write(",".join(cells))
or replace:
rows = re.split('\n', f.read())
with
rows = f.readlines()
and
receipt.write(cells)
with
receipt.write(row)
The first example joins the elements of cells back into a string, inserting a comma between each one. The second one means that rows is a list containing all the rows of the input file, rather than an iterator that reads one row at a time. The first method is probably better as it avoids reading a large file into memory, just as long as you realise what it means to get an iterator rather than a list.
write call in receipt.write(cells) expects a string, whereas you give it a list.
You can use join if you want everything to be concatenated (in this example, the values would be separated by dashes) :
receipt.write('-'.join(cells))
Hope it'll be helpful

Parsing unique values in a CSV where the primary key is not unique

This seems pretty trivial. Generally, I'd do something like the following:
results = []
reader = csv.reader(open('file.csv'))
for line in reader: # iterate over the lines in the csv
if line[1] in ['XXX','YYY','ZZZ']: # check if the 2nd element is one you're looking for
results.append(line) # if so, add this line the the results list
However, my data set isn't so simply formatted. It looks like the following:
Symbol,Values Date
XXX,8/2/2010
XXX,8/3/2010
XXX,8/4/2010
YYY,8/2/2010
YYY,8/3/2010
YYY,8/4/2010
ZZZ,8/2/2010
ZZZ,8/3/2010
ZZZ,8/4/2010
Essentially what I am trying to do is parse the first date for each unique Symbol in the list such that I end up with the following:
XXX,8/2/2010
YYY,8/2/2010
ZZZ,8/2/2010
Pandas may help. ;-)
import pandas
pandas.read_csv('file.csv').groupby('Symbol').first()
Here is a simple solution using a set of already found 1st element:
results = []
reader = csv.reader(open('file.csv'))
already_done = set()
for line in reader: # iterate over the lines in the csv
if line[1] in ['XXX','YYY','ZZZ'] and line[0] not in already_done:
results.append(line) # if so, add this line the the results list
already_done.add(line[0])

reading a file and parse them into section

okay so I have a file that contains ID number follows by name just like this:
10 alex de souza
11 robin van persie
9 serhat akin
I need to read this file and break each record up into 2 fields the id, and the name. I need to store the entries in a dictionary where ID is the key and the name is the satellite data. Then I need to output, in 2 columns, one entry per line, all the entries in the dictionary, sorted (numerically) by ID. dict.keys and list.sort might be helpful (I guess). Finally the input filename needs to be the first command-line argument.
Thanks for your help!
I have this so far however can't go any further.
fin = open("ids","r") #Read the file
for line in fin: #Split lines
string = str.split()
if len(string) > 1: #Seperate names and grades
id = map(int, string[0]
name = string[1:]
print(id, name) #Print results
We need sys.argv to get the command line argument (careful, the name of the script is always the 0th element of the returned list).
Now we open the file (no error handling, you should add that) and read in the lines individually. Now we have 'number firstname secondname'-strings for each line in the list "lines".
Then open an empty dictionary out and loop over the individual strings in lines, splitting them every space and storing them in the temporary variable tmp (which is now a list of strings: ('number', 'firstname','secondname')).
Following that we just fill the dictionary, using the number as key and the space-joined rest of the names as value.
To print the dictionary sorted just loop over the list of numbers returned by sorted(out), using the key=int option for numerical sorting. Then print the id (the number) and then the corresponding value by calling the dictionary with a string representation of the id.
import sys
try:
infile = sys.argv[1]
except IndexError:
infile = input('Enter file name: ')
with open(infile, 'r') as file:
lines = file.readlines()
out = {}
for fullstr in lines:
tmp = fullstr.split()
out[tmp[0]] = ' '.join(tmp[1:])
for id in sorted(out, key=int):
print id, out[str(id)]
This works for python 2.7 with ASCII-strings. I'm pretty sure that it should be able to handle other encodings as well (German Umlaute work at least), but I can't test that any further. You may also want to add a lot of error handling in case the input file is somehow formatted differently.
Just a suggestion, this code is probably simpler than the other code posted:
import sys
with open(sys.argv[1], "r") as handle:
lines = handle.readlines()
data = dict([i.strip().split(' ', 1) for i in lines])
for idx in sorted(data, key=int):
print idx, data[idx]

Python String Formatting, need to continuously add to a string

I just started python, any suggestions would be greatly appreciated. I'm reading a table generated by another program and pulling out 2 numbers from each line, I'll call them a and b. (they are saved as flux and observed in my program) I need to take these two numbers from each line and format them like this-
(a,b),(a,b),(a,b) ect.
Each consecutive parenthesis is from the consecutive line, first a,b is from line 1, second a,b is from line 2, etc. I need to read the entire table however, the table length will vary.
This is what I have so far. It can read the table and pull out the numbers I need, however, I don't know how to put the numbers into the proper format. I want to say something recursive would be most efficient but I'm unsure of how to do that. Thank you in advance.
#!/usr/bin/python
file = open("test_m.rdb")
while 1:
line = file.readline()
i = line.split()
flux = i[2]
observed = i[4]
if not line:
break
with open("test_m.rdb") as inf:
results = [(i[2],i[4]) for i in (line.split() for line in inf)]
result_string = ",".join(str(res) for res in results)
or a more general formatter:
result_string = ", ".join("('{2}', '{4}')".format(*res) for res in results)
Very simple solution:
with open('data') as data:
print ', '.join('(%s, %s)' % (x.split()[2], x.split()[4])
for x in data.readlines())
Just use readlines to iterate over the lines in the file.
assuming that two values you got are str1 and str2
//inside a loop which iterates through your values
strTable = strTable + ['('+str1+')','('+str2+')']
hope oit will work, if it dont, comment , i will solve it.

Categories

Resources