How do I read different csv files in folder without concatenating them but just assigning them with the original file name? For example, file with path …table1.csv will be named “table1” I managed to get all file names how do I read each file now? In other words, instead of reading multiple csv files in pandas like this: is there
Tag: string-formatting
python str.format with utf-8 characters that take more than 1 position
I trying to print japanese characters in python, aligned in columns. It seems that japanese characters have a width equivalent to two spaces, so the alignment doesn’t work. Here is the code: The output I obtain is the following: The column in the left is spanish but that’s not important. The important thing is that the 3 columns on the
Correct way to format integers with fixed length and space padding
I have integer numbers in the range of 0 up until (including) 100. I want to convert them to strings of a fixed length of 3 with space padding and alignment to the right. I’ve tried to use the following format string, but it adds another space for three digit numbers, which makes them length 4 instead of 3. Interestingly,
Think Python 2nd Edition Exercise 7-1
“Square Roots” loop: Copy the loop from “Square Roots” and encapsulate it in a function called mysqrt that takes a as a parameter, chooses a reasonable value of x, and returns an estimate of the square root of a. To test it, write a function named test_square_root that prints a table like this: Here’s what I wrote: Here’s what I
Why round off of 0.500000 in python differs from 45.500000 using ‘%.0f’?
Recently, I learned art of string formatting in Python 2.7. I decided to play with floating point numbers. Came across an awkward looking solution, as written below. BUT Please help me understand, why this behavior is shown by %f. Answer The internal implementation for the %.0f string format uses a round-half-even rounding mode. In Python 2, the round() function uses
How to turn a list/tuple into a space separated string in python using a single line?
I tried doing: but it says str object is not callable. Is this doable using a single line? Answer Use string join() method. List: Tuple: Non-string objects:
format strings and named arguments in Python
Case 1: It will give KeyError: ‘arg1′ because I didn’t pass the named arguments. Case 2: Now it will work properly because I passed the named arguments. And it prints ’10 20’ Case 3: And, If I pass wrong name it will show KeyError: ‘arg1’ But, Case 4: If I pass the named arguments in wrong order It works… and
remove leading 0 in exponential format in python
I have the following code in Python 3.1 Which gives a string of 9.038768E-08, but I want the string 9.038768E-8 with the leading 0 of E-08 removed. How should I go about this? Answer you could do something like this or better as JBernardo suggests You need to do a replace for E+0 as well if you have big numbers
Using locals() and format() method for strings: are there any caveats?
Are there any disadvantages, caveats or bad practice warnings about using the following pattern? I had a very repetitive string generation code to write and was tempted to use this, but something about using locals() makes me uncomfortable. Is there any danger of unexpected behavior in this? Edit: context I found myself constantly writing stuff like: Answer There is now
Format a datetime into a string with milliseconds
How can I format a datetime object as a string with milliseconds? Answer To get a date string with milliseconds, use [:-3] to trim the last three digits of %f (microseconds): Or slightly shorter: