dynamically creating key and values in dictionary in Python - python-3.x

I am new to Python. I am here reading several lines in a file having a format
121212-pr-ba-su_re;m1;1;10000
I have extracted (pr-ba-su) content using split function. Please see the code which i wrote. I wish to store the Prefix-base-suffix as a key and its value as ID (which is extracted in the first line of loop) in a dictionary. (I want to replace the key with value in another list. Therefore, i am trying to create a dictionary.)
data ={}
f = open('var_test.txt')
for line in f:
part,m,t,ID = line.split(';',3)
partnumb,r = part.split('_',1)
item,prefix,base,suffix =partnumb.split('-',3)
data[prefix'-'base'-'suffix] =ID
But i am getting an error as Syntax Error
data(getattr([prefix'-'base'-'suffix])) =PID
^SyntaxError: invalid syntax
I have tried using getattr()function,and update function but no avail.Can anybody help me? Thanks a lot.

The line data[prefix'-'base'-'suffix] =ID is your problem. It is not valid Python syntax.
You could get the results you expect by substituting for this line
data["{}-{}-{}".format(prefix, base, suffix)] = ID

Related

F string is adding new line

I am trying to make a name generator. I am using F string to concatenate the first and the last names. But instead of getting them together, I am getting them in a new line.
print(f"Random Name Generated is:\n{random.choice(firstname_list)}{random.choice(surname_list)}")
This give the output as:
Random Name Generated is:
Yung
heady
Instead of:
Random Name Generated is:
Yung heady
Can someone please explain why so?
The code seems right, perhaps could be of newlines (\n) characters in element of list.
Check the strings of lists.
import random
if __name__ == '__main__':
firstname_list = ["yung1", "yung2", "yung3"]
surname_list = ["heady1", "heady2", "heady3"]
firstname_list = [name.replace('\n', '') for name in firstname_list]
print(f"Random Name Generated is:\n{random.choice(firstname_list)} {random.choice(surname_list)}")
Output:
Random Name Generated is:
yung3 heady2
Since I had pulled these values from UTF-8 encoded .txt file, the readlines() did convert the names to list elements but they had a hidden '\xa0\n' in it.
This caused this particular printing problem. Using .strip() helped to remove the spaces.
print(f"Random Name Generated is:\n{random.choice(firstname_list).strip()} {random.choice(surname_list).strip()}")

list() definition TypeError

I have a problem while creating an empty list in Python3.6.3 with list() (I work with Pycharm - community 2019.3)
The strange thing is that if I do it in terminal (or if I clear all the script code but this line) it works, but if I use the same line in a script, it's highlighted as an error and running it would lead to TypeError: 'list' object is not callable
The code up to the point it fails is is:
list = open('intro.txt').read().split()
alph = "abcdefgjklmn'opqrstuvwxyz"
alph = alph + alph.upper()
clean_list = list()
The 4th line is the one that fails, and it does the same everytime I generate an empty list this way..
Btw I have no problem by doing clean_list = [ ] but this errror annoys me a lot
Thanks!
You already have a variable called "list" which Python won't like if used in the same script as list(). So try renaming the the first variable.

Separating header from the rest of the dataset

I am reading in a csv file and then trying to separate the header from the rest of the file.
hn variable is is the read-in file without the first line.
hn_header is supposed to be the first row in the dataset.
If I define just one of these two variables, the code works. If I define both of them, then the one written later does not contain any data. How is that possible?
from csv import reader
opened_file = open("hacker_news.csv")
read_file = reader(opened_file)
hn = list(read_file)[1:] #this should contain all rows except the header
hn_header = list(read_file)[0] # this should be the header
print(hn[:5]) #works
print(len(hn_header)) #empty list, does not contain the header
The CSV reader can only iterate through the file once, which it does the first time you convert it to a list. To avoid needing to iterate through multiple times, you can save the list to a variable.
hn_list = list(read_file)
hn = hn_list[1:]
hn_header = hn_list[0]
Or you can split up the file using extended iterable unpacking
hn_header, *hn = list(read_file)
Just change below line in your code, no additional steps needed. read_file = list(reader(opened_file)). I hope now your code is running perfectly.
The reader object is an iterator, and by definition iterator objects can only be used once. When they're done iterating you don't get any more out of them.
You can refer more about from this Why can I only use a reader object once? question and also above block-quote taken from that question.

adding getmtime() to db and getting TypeError: 'float' object is not iterable

I'm trying to add all the file names and last modification time for files in a specific directory to a database in two different columns. This is code I have:
def getlist (self):
dbinput = self.txt_dest.get()
conn = sqlite3.connect ('dirdrill.db')
for files in os.listdir(dbinput):
for modtime in os.path.getmtime(dbinput):
with conn:
cur = conn.cursor()
cur.execute("INSERT INTO tbl_files(col_filename, col_modinfo) values (?,?)", files, modtime)
conn.commit()
conn.close()
getinfo(self)
I am getting the following error:
File "C:\Users\Ryan\Documents\GitHub\Tech-Academy-Projects\Python\dir_drill\dirdrill_func.py", line 69, in getlist
for modtime in os.path.getmtime(dbinput):
TypeError: 'float' object is not iterable
Do I need to convert the numbers that getmtime() returns into a human friendly format so that it is not a float? If so, how? Or should I be structuring the function a different way?
EDIT:
Thanks to some other help I found out that I just needed to change one line to modinfo = os.path.getmtime(dbinput). Did that and it solved the float error. However now it says that I am offering too many arguments, max 2 and I have 3, even thought I only see two. Onto the next bug... :-)
Found the answer to the original question and the additional error I discovered.
In the original code I needed to change the following:
for modtime in os.path.getmtime(dbinput):
to:
modtime = os.path.getmtime(dbinput)
For the follow up error related to too many parameters, files and modinfo needed to be wrapped in parenthesis like so:
cur.execute("INSERT INTO tbl_files(col_filename, col_modinfo) values (?,?)", (files, modtime))
Hopes this helps anyone else experiencing the same error.

Read from URL & Process data using list comprehension

I am new to python and I am trying to read data from URL. Basically I am reading the historical stock data, get the closing price and save the closing price in to a list. The closing price is available at the 4th index (5th column) of each line. And I want to do all of these within a list comprehension.
Code snippet:
from urllib.request import urlopen
URL = "http://ichart.yahoo.com/table.csv?s=AAPL&a=3&b=1&c=2016&d=9&e=30&f=2016"
def downloadClosingPrice():
urlHandler = urlopen(URL)
next(urlHandler)
return [float(line.split(",")[4]) for line in urlHandler.read().decode("utf8").splitlines() if line]
closingPriceList = downloadClosingPrice()
The above code just works fine. I am able to read and fetch the required data. However just out of curiosity, can the code for list comprehension be written in a more simpler or easier way ?
Thanks...
I did try out various ways and this is how I could do the same using different forms of list comprehension:
return [float(line.decode("utf8").split(",")[4]) for line in urlHandler if line]
# return [float(line.decode("utf8").split(",")[4]) for line in urlHandler.readlines() if line]
# return [float(line.split(",")[4]) for line in urlHandler.read().decode("utf8").splitlines() if line]
The first one is better because it reads the file line by line which saves memory. And of course it's simpler and easier to understand.

Resources