This question already has answers here:
How to use python numpy.savetxt to write strings and float number to an ASCII file?
(3 answers)
Closed 6 years ago.
I am trying to save my data into csv file and I would like to specify different data types for different columns I am saving (e.g int for id, string for name), how can I do that?
For now with the following method I am able to save all 3 arrays as string.
d = np.column_stack((id, first_name, surname ))
np.savetxt('table.csv', d, delimiter=',', fmt="%s")
Thank you!
If all you're looking for is simply different way to format the output strings, you can look at the documentation (https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html):
fmt : str or sequence of strs, optional
A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is
ignored.
Just pass fmt as a list of formats, e.g. fmt=("%d", "%s", "%s").
Related
This question already has answers here:
How can I convert a string with dot and comma into a float in Python
(9 answers)
Closed 5 months ago.
I am given a csv file which contains numbers ranging from 800 to 3000. The problem is numbers greater than thousand has a comma in them e.g. 1,227 or 1,074 or 2,403.
When I want to calculate their mean, variance or standard deviation using scipy or numpy, I get error: ValueError: could not convert string to float: '1,227'. How convert them to numbers so that I could do calculations on them. CSV file should not be changed as it is read only file.
Thanks, guys! I fixed it by using replace function. hpaulj's link was useful.
my_string=[val[2] for val in csvtext]
my_string=[x.replace(',', '') for x in my_string]
my_float=[float(i) for i in my_string]
This is the code, in which, 1st line loads csv string list to my_string and 2nd line removes comma and 3rd line produces numbers that are easy for calculation. So, there is no need for editing the file or creating a new one. Just a list manipulation will do the job.
This really is a locale issue, but a simple solution would be to simply call replace on the string first:
a = '1,274'
float(a.replace(',','')) # 1274.0
Another way is to use pandas to read the csv file. Its read_csv function has a thousands argument.
If you do know something about the locale, then it's probably best to use the locale.atof() function
This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Closed 6 years ago.
I have make a set of names in this form: 's1', 's2', ..., 's100'. I thought I can do that easily via looping:
for i in range(100):
print ('s'.format(i+1))
format here does not append the numbers. I only get ss..ss without the numbers being concatenated in single quote. I know how to do this in Java but I am not that much expert in Python. Thank you
You need to have a placeholder in the format string:
Perform a string formatting operation. The string on which this method
is called can contain literal text or replacement fields delimited by
braces {}. Each replacement field contains either the numeric index of
a positional argument, or the name of a keyword argument.
for i in range(100):
print ('s{0}'.format(i+1))
If you use 3.6, then you can take advantage of the new 'Literal String Interpolation', and do the following:
for i in range(100):
print(f's{i + 1}')
For more details on this feature, check out PEP 498 -- Literal String Interpolation
This question already has answers here:
Format output string, right alignment
(8 answers)
Closed 8 years ago.
I have a tuple contains both string and float values (which are read from a txt file and calculated by me, respectively) and I want to write it to another txt file.
variables = (line.split()[0],line.split()[1], velocity) #velocity is a floating number, others are #string
output_file.write('%s %s %4.2f \n' % variables)
These lines are in a for loop. I want to align each variable in each line as right justified. How can I do that?
Please note that string items don't have same character in each line.
Python has several ways to format strings. In the form you use, you get right alignment by specifying a field length and optional padding. Python right aligns to fit the field length by default. Your float calculation already has a field length, so just decide on a length for the strings also. Its easy if you already have a max field size in mind. Here is an example of 10 spaces per string:
'%10s %10s %4.2f \n' % variables
This question already has answers here:
Suppress the u'prefix indicating unicode' in python strings
(11 answers)
Closed 8 years ago.
I want to go through data in my folder, identify them and rename them according to a list of rules I have in an excel spreadsheet
I load the needed libraries,
I make my directory the working directory;
I read in the xcel file (using xlrd)
and when I try to read the data by columns e.g. :
fname = metadata.col_values(0, start_rowx=1, end_rowx=None)
the list of values comes with a u in front of them - I guess unicode - such as:
fname = [u'file1', u'file2'] and so on
How can I convert fname to a list of ascii strings?
I'm not sure what the big issue behind having unicode filenames is, but assuming that all of your characters are ascii-valid characters the following should do it. This solution will just ignore anything that's non-ascii, but it's worth thinking about why you're doing this in the first place:
ascii_string = unicode_string.encode("ascii", "ignore")
Specifically, for converting a whole list I would use a list comprehension:
ascii_list = [old_string.encode("ascii", "ignore") for old_string in fname]
The u at the front is just a visual item to show you, when you print the string, what the underlying representation is. It's like the single-quotes around the strings when you print that list--they are there to show you something about the object being printed (specifically, that it's a string), but they aren't actually a part of the object.
In the case of the u, it's saying it's a unicode object. When you use the string internally, that u on the outside doesn't exist, just like the single-quotes. Try opening a file and writing the strings there, and you'll see that the u and the single-quotes don't show up, because they're not actually part of the underlying string objects.
with open(r'C:\test\foo.bar', 'w') as f:
for item in fname:
f.write(item)
f.write('\n')
If you really need to print strings without the u at the start, you can convert them to ASCII with u'unicode stuff'.encode('ascii'), but honestly I doubt this is something that actually matters for what you're doing.
You could also just use Python 3, where Unicode is the default and the u isn't normally printed.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Integers from excel files become floats?
I have an excel spreadsheet that contains 1984, which xlrd handles as a number type, and thus gives me the value back as the float 1984.0. I want to get the original value as it appears in the spreadsheet, as a string "1984". How do I get this?
So internally in Excel, that 1984 is stored as a decimal number, so 1984.0 is correct. You could have changed the number formatting to show it as 1984.00, or whatever.
So are you asking how to query the cell formatting to tell that the number format is no decimals? If so you might look into using the formatting_info=True parameter of open_workbook
sheet = open_workbook(
'types.xls',formatting_info=True
).sheet_by_index(0)
Have you come across the python-excel.pdf document from http://www.python-excel.org/ ?
It is pretty good tutorial for learning to use xlrd and xlwt. Unfortunately, they say:
We've already seen that open_workbook has a parameter to load formatting information from Excel files. When this is done, all the formatting information is available, but the details of how it is presented are beyond the scope of this tutorial.
if cell.ctype==xlrd.XL_CELL_NUMBER
then excel is storing 1984 as a float and you would need to convert to a string in python
In excel
="1984" would be a string
'1984 would be a string, note that ' does not display
1984 is a #
The only kind of number is a float. The formatting attached to the cell determines if it represents a date, a decimal, or an integer. Look up the format string, and hopefully it will let you discern how the number is to be displayed.
Use string formatting:
"%d" % mynumber
>>> "%d" % 1984.0
'1984'