I just started to learn python. I need to store a csv file data into a list of tuples: tuples to represent the values on each row, list to store all the rows.
The function I have problem with is when I need to filter the list. Basically create a copy of the list with only the ones that met criteria. I have successfully appended all the tuples into a list, but when I need to append the tuples into a new list, it doesn't work.
def filterRecord():
global filtered
filtered = list()
try:
if int(elem[2])>= int(command[-1]): #the condition
#if I print(elem) here, all results are correct
filtered.append(tuple(elem)) #tuples do not add into the list
#len(filtered) is 0
except ValueError:
pass
def main():
infile = open('file.csv')
L = list()
for line in infile:
parseLine() #a function store each row into tuple
for line in stdin:
command = line.split() #process user input, multiple lines
for elem in L:
if command == 0:
filterRecord()
If I run it, the program doesn't response. If I force stop it, the traceback is always for line in stdin
Also, I am not allowed to use the csv module in this program.
I think you need to import sys and use for line in sys.stdin
You should use python's built-in library to parse csv files (unless this is something like a homework assignment): https://docs.python.org/2/library/csv.html.
You can then do something like:
import csv
with open ('file.csv', 'r') as f:
reader = csv.DictReader(f, delimiter=",")
Related
I am trying to concatenate a string to send a message via python>telegram
My plan is so that the function is modular.
It first import lines from a .txt file and based on that many lines it creates two different arrays
array1[] and array2[], array1 will receive the values of the list as strings and array2 will receive user generated information to complemente what is stored in the same position as to a way to identify the differences in the array1[pos], as to put in a way:
while (k<len(list)):
array2[k]= str(input(array1[k]+": "))
k+=1
I wanted to create a single string to send in a single message like however in a way that all my list goes inside the same string
string1 = array1[pos]+": "+array2[pos]+"\n"
I have tried using while to compared the len but I kept recalling and rewriting my own string again and again.
It looks like what you're looking for is to have one list that comes directly from your text file. There's lots of ways to do that, but you most likely won't want to create a list iteratively with the index position. I would say to just append items to your list.
The accepted answer on this post has a good reference, which is basically the following:
import csv
with open('filename.csv', 'r') as fd:
reader = csv.reader(fd)
for row in reader:
# do something
Which, in your case would mean something like this:
import csv
actual_text_list = []
with open('filename.csv', 'r') as fd:
reader = csv.reader(fd)
for row in reader:
actual_text_list.append(row)
user_input_list = []
for actual_text in actual_text_list:
the_users_input = input(f'What is your response to {actual_text}? ')
user_input_list.append(the_users_input)
This creates two lists, one with the actual text, and the other with the other's input. Which I think is what you're trying to do.
Another way, if the list in your text file will not have duplicates, you could consider using a dict, which is just a dictionary, a key-value data store. You would make the key the actual_text from the file, and the value the user_input. Another technique, you could make a list of lists.
import csv
actual_text_list = []
with open('filename.csv', 'r') as fd:
reader = csv.reader(fd)
for row in reader:
actual_text_list.append(row)
dictionary = dict()
for actual_text in actual_text_list:
the_users_input = input(f'What is your response to {actual_text}? ')
dictionary[actual_text] = the_users_input
Then you could use that data like this:
for actual_text, user_input in dictionary.items():
print(f'In response to {actual_text}, you specified {user_input}.')
list_of_strings_from_txt = ["A","B","C"]
modified_list = [f"{w}: {input(f'{w}:')}" for w in list_of_strings_from_txt]
I guess? maybe?
Organization scheme of the data in the csv file:
Lisbon,Madrid,600
Madrid,Paris,650
import csv
with open('cidades.csv', 'r') as f: # Somente r para leitura do arquivo
read = csv.DictReader(f)
list1 =[]
for line in read:
list1.append=tuple(line.values())
I want to store these tuples all in a list and then I use the "distance" attribute to calculate the smallest path between two cities
It returns an error passing the tuple to the end of the list. AttributeError: 'list' object attribute 'append' is read-only
of course:
list1.append=tuple(line.values())
here you're trying to redefine the append method of list as a tuple. Fortunately for you, list members are protected against redefinition, which explains the error.
This is a typo: you should have done:
list1.append(tuple(line.values()))
better: get rid of the loop and read list1 in one go using list comprehension:
with open('cidades.csv', 'r') as f:
list1 = [tuple(line.values()) for line in csv.DictReader(f)]
but this approach still makes me dubious: reading the values loses infomation about the order (unless you're using Python 3.6 which uses OrderedDict for csv to fix this issue). I would drop DictReader and rely on the order of your csv using csv.reader, like this:
with open('cidades.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # skip the title line we don't need it
list1 = [tuple(line) for line in reader]
I have been trying to append to a list inside of another file, I also am trying to make it so if it has more than 3 variables inside of it, it deletes the oldest added variable and adds the new data, it confuses me pretty badly and this is the code I have so far:
with open ("TestScores_Class1.txt","ab") as a:
Class1Score = [name, points]
Class1Scorelen = (Class1Score,a)
if len(Class1Scorelen) > 3:
del (Class1Score,a)[3]
pickle.dump(Class1Score,a)
a.close()
Try to break your program up into small logical segments. You're trying to do three things:
load a list from file
modify a list
save a list to file
Clearly separating each action should simplify things.
import pickle
to_add = ("Kevin", 42)
#Open the file and read its contents.
#If the file is blank or doesn't exist, make an empty list.
try:
with open("my_file.txt") as file:
data = pickle.load(file)
except (EOFError, IOError):
data = []
#add the item to the list. Shorten the list if it's too long.
data.append(to_add)
if len(data) > 3:
data = data[-3:]
#Overwrite the file with the new data.
with open("my_file.txt", "w") as file:
pickle.dump(data, file)
I am new to Python, so please bear with me.
I can't get this little script to work properly:
genome = open('refT.txt','r')
datafile - a reference genome with a bunch (2 million) of contigs:
Contig_01
TGCAGGTAAAAAACTGTCACCTGCTGGT
Contig_02
TGCAGGTCTTCCCACTTTATGATCCCTTA
Contig_03
TGCAGTGTGTCACTGGCCAAGCCCAGCGC
Contig_04
TGCAGTGAGCAGACCCCAAAGGGAACCAT
Contig_05
TGCAGTAAGGGTAAGATTTGCTTGACCTA
The file is opened:
cont_list = open('dataT.txt','r')
a list of contigs that I want to extract from the dataset listed above:
Contig_01
Contig_02
Contig_03
Contig_05
My hopeless script:
for line in cont_list:
if genome.readline() not in line:
continue
else:
a=genome.readline()
s=line+a
data_out = open ('output.txt','a')
data_out.write("%s" % s)
data_out.close()
input('Press ENTER to exit')
The script successfully writes the first three contigs to the output file, but for some reason it doesn't seem able to skip "contig_04", which is not in the list, and move on to "Contig_05".
I might seem a lazy bastard for posting this, but I've spent all afternoon on this tiny bit of code -_-
I would first try to generate an iterable which gives you a tuple: (contig, gnome):
def pair(file_obj):
for line in file_obj:
yield line, next(file_obj)
Now, I would use that to get the desired elements:
wanted = {'Contig_01', 'Contig_02', 'Contig_03', 'Contig_05'}
with open('filename') as fin:
pairs = pair(fin)
while wanted:
p = next(pairs)
if p[0] in wanted:
# write to output file, store in a list, or dict, ...
wanted.forget(p[0])
I would recommend several things:
Try using with open(filename, 'r') as f instead of f = open(...)/f.close(). with will handle the closing for you. It also encourages you to handle all of your file IO in one place.
Try to read in all the contigs you want into a list or other structure. It is a pain to have many files open at once. Read all the lines at once and store them.
Here's some example code that might do what you're looking for
from itertools import izip_longest
# Read in contigs from file and store in list
contigs = []
with open('dataT.txt', 'r') as contigfile:
for line in contigfile:
contigs.append(line.rstrip()) #rstrip() removes '\n' from EOL
# Read through genome file, open up an output file
with open('refT.txt', 'r') as genomefile, open('out.txt', 'w') as outfile:
# Nifty way to sort through fasta files 2 lines at a time
for name, seq in izip_longest(*[genomefile]*2):
# compare the contig name to your list of contigs
if name.rstrip() in contigs:
outfile.write(name) #optional. remove if you only want the seq
outfile.write(seq)
Here's a pretty compact approach to get the sequences you'd like.
def get_sequences(data_file, valid_contigs):
sequences = []
with open(data_file) as cont_list:
for line in cont_list:
if line.startswith(valid_contigs):
sequence = cont_list.next().strip()
sequences.append(sequence)
return sequences
if __name__ == '__main__':
valid_contigs = ('Contig_01', 'Contig_02', 'Contig_03', 'Contig_05')
sequences = get_sequences('dataT.txt', valid_contigs)
print(sequences)
The utilizes the ability of startswith() to accept a tuple as a parameter and check for any matches. If the line matches what you want (a desired contig), it will grab the next line and append it to sequences after stripping out the unwanted whitespace characters.
From there, writing the sequences grabbed to an output file is pretty straightforward.
Example output:
['TGCAGGTAAAAAACTGTCACCTGCTGGT',
'TGCAGGTCTTCCCACTTTATGATCCCTTA',
'TGCAGTGTGTCACTGGCCAAGCCCAGCGC',
'TGCAGTAAGGGTAAGATTTGCTTGACCTA']
I am strugling with this problem that looks simple but I'm stuck! Well, I have to build a function where I receive a list of categories like:
input Example1: ['point_of_interest', 'natural_feature', 'park', 'establishment']
input Example2: ['point_of_interest', 'establishment']
input Example3: ['sublocality', 'political']
So that list could be with variable elements inside I guess from 1 till 4 not more
So with this same data I am gonna create a file with that input in a way that if the new input is not in the file, append it to the file.
The way is each list is an element itself, I mean I have to compare the full elements of the list and if I can find other list exactly equal I don´t have to add it.
In my code I just tried to add the first element in the file because really I don't know how to add the full list to compare with the next list.
def categories(category):
number = 0
repeat = False
if os.path.exists("routes/svm/categories"):
with open('routes/svm/categories', 'rb') as csvfile:
spamreader = csv.reader(csvfile)
for categoryFile in spamreader:
if (cmp(categoryFile,category) == 0):
number += 1
repeat = True
if not repeat:
categoriesFile = open('routes/svm/categories', 'a')
category = str(category[0])
categoriesFile.write(category)
categoriesFile.write('\n')
categoriesFile.close()
else:
categoriesFile = open('routes/svm/categories', 'w')
category = str(category[0])
categoriesFile.write(category)
categoriesFile.write('\n')
categoriesFile.close()
EDIT: Some explanation by #KlausWarzecha: Users might enter a list with (about 4) items. If this list ( = this combination of items) is not in the file already, you want to add the list (and not the items separately!) to the file? –
The problem is really simple. You may take the following approach if it works for you:
Read all the contents of the CSV into a list
Add all the non-matching items from the input into this list
re-write the CSV file
You may start with this sample code:
# input_list here represents the inputs
# You may get input from some other source too
input_list = [['point_of_interest', 'natural_feature', 'park', 'establishment'], ['point_of_interest', 'establishment'], ['sublocality', 'political']]
category_list = []
with open('routes/svm/categories', 'rb') as csvfile:
spamreader = csv.reader(csvfile)
for categoryFile in spamreader:
print categoryFile
category_list.append(categoryFile)
for item in input_list:
if (item in category_list):
print "Found"
else:
category_list.append(item)
print "Not Found"
# Write `category_list` to the CSV file
Please use this code as a starting point and not as a copy-paste solution.