python 3.x readlines() - python-3.x

After a pause I've currently started to work with python again but right at the start I encountered an annoying (and at least for me not solvable...) problem. I want to open a normal .txt file with tabular content so I can iterate over specific 'columns' to gather all the information I need. The problem is that I don't get each line of the document as a list but instead python creates strings of each line.I also tried .readlines() but thats doesn't work either.
I work on a Win7 PC and the code goes as followed:
with open('C:\\filepath...\\file.txt') as file:
for f in file:
print(f[0])
I also have to add that I also worked with python in the past and never encountered such problem so if anyone knows a solution I would really appreciate some help. Thank you in advance.

you just need to split:
TheList = []
with open('C:\\filepath...\\file.txt') as file:
for f in file:
TheList.append(f.split('\t'))

Related

Empty file being created by node-gpg

I'm trying to use node-gpg: https://github.com/drudge/node-gpg for encrypting/decrypting files using GPG.
snippet of code
a file is being created in the fileDecrypted location but doesn't contain anything
Does anyone know what might be wrong/have encountered a similar issue?
Any help would be appreciated
I also looked into this implementation: https://jaygould.co.uk/2019-01-21-decrypting-gpg-file-node-programatically/
and am doing the same steps but still running into the same problem

Can you read the length of an mp3 file in python 3 on windows 10?

I am currently creating a music player in python 3.3 and I have a way of opening the mp3/wav files, namely through using through 'os.startfile()', but, this way of running the files means that if I run more than one, the second cancels the first, and the third cancels the second, and so on and so forth, so I only end up running the last file. So, basically, I would like a way of reading the mp3 file length so that I can use 'time.sleep(SongLength)' between the start of each file.
Thanks in advance.
EDIT:
I forgot to mention, but I would prefer to do this using only pre-installed libraries, as i am hoping to publish this online as a part of a (much) larger program
i've managed to do this Using an external module, as after ages of trying to do it without any, i gave up and used tinytag, as it is easy to install and use.
Nothing you can do without external libraries, as far as I know. Try using pymad.
Use it like this:
import mad
SongFile = mad.MadFile("something.mp3")
SongLength = SongFile.total_time()

Output generated is different with output in NLTK tutorial

Today I start to learn Python because I need to use NLTK in my assignment. In order to learn it, I follow the tutorial in this site http://www.nltk.org/book/ch01.html. However, when I run the programme in Python interpreter, the output produced is not same with what has been shown in the website and I have no idea on what thing that this output wants to tell me.
(Below is the picture of the output:)
Input: >>> from nltk.book import *
Output (After I hit 'Enter'):
So now my questions are what is the error about and if there is a way to solve it, then what should I need to do?
Thanks for looking into my problem.
This appears to be a known bug with nltk and Python 3. It seems to have been fixed within the past two weeks, but I expect you'll have to wait until there's a release that contains the fix. You could try installing from source.

Any tips on creating a Pygame Developer Console?

I've been getting my feet wet with Python and Pygame, and after a few bugs in a very basic game that I have been making, I've thought to myself that having some sort of console that can deal with string input (from a developer) would be very handy.
Here's an example:
I notice the player disappears after double jumping. So, in the console that I will hopefully create, I would type in:
>> report_bug("Player disappears after double jumping")
Where report_bug(string) is a user-defined function that will do the following things:
report_bug(string):
# Check if debug file already exists; create new file if none exists
# Append string to file, along with other extra info
Could someone point me in the right direction? Will I have to create something from scratch, and if so, how would I go about doing that? I've attempted to modify the source code from: http://www.pygame.org/project-pygame-console-287-.html, however it is 9 years-old and made for Python 2.x, when I'm using Python 3.4. Any help would be greatly appreciated.
I've managed to get the 2.X PygameConsole version 0.7 working, so I will stick with that!
Edit: I will attempt to port PyConsole to Python 3.X and hopefully add some more features so newcomers can easily understand what is going on.

FTP.retrbinary fails

I'm a complete Python novice, so I apologize if the solution to my problem seems obvious. I'm having difficulty with some relatively simple code that I've written. I've scanned several related questions that have already been posted, but I don't see where my code differs in any meaningful way from the solutions suggested.
I'm trying to write a program that will:
Establish a ftp connection to a remote server.
Change the working directory on the ftp server.
Retrieve a list of files in the working directory from the ftp server.
Find a file ending with a specific suffix from the retrieved list of files.
Retrieve the found file to a temporary directory (created by tempfile.mkdtemp()) on the user's local file system.
Steps 1 through 4 are working as expected. Sadly, the last step is falling into my except clause.
Can anyone make a suggestion regarding what might be wrong with the following line of code?
ftp.retrbinary('RETR ' + file, open(opsys.path.join(localTempDir, fileName)).write)
Your suggestions are greatly appreciated. Thanks, in advance.
Possible problems:
- type(file) = incorrect value
- opsys.path.join(localTempDir, fileName) = incorrect value #nonexistent file
Thats all what comes to mind looking on presented line of code =)

Resources