Write Number as string csv python - python

I'm trying to write a csv file from json data. During that, i want to write '001023472' but its writing as '1023472'. I have searched a lot. But dint find an answer.
The value is of type string before writing. The problem is during writing it into the file.
Thanks in advance.

Convert the number to string with formatting operator; in your case: "%09d" % number.

Use the format builtin or format string method.
>>> format(1023472, '09')
'001023472'
>>> '{:09}'.format(1023472)
'001023472'
If your "number" is actually a string, you can also just left-pad it with '0''s:
>>> format('1023472', '>09')
'001023472'
The Python docs generally eschew % formatting, saying it may go away in the future and is also more finnicky; for new code there is no real reason to use it, especially in 2.7+.

Related

Is there a way to convert a python dictionary string with some values as python commands?

How to convert this to a dictionary?
params = "{'cat_features':[X.columns.get_loc(i) for i in cat_vars],
'num_boost_round':100, 'eta':.01, 'reg_lambda':1.8, 'verbose':False,
'loss_function':'MultiClass','early_stopping_rounds':5}"
without the first part [X.columns.get_loc(i) for i in cat_vars] I can run ast.literal_eval(), but that doesn't work if there is python code in the string. Any idea how to solve this?
You can use plain eval.
However, using eval is is risky if the string comes from an non-trusted source because a properly crafted string could execute anything on the computer where the program is running.

Issues relating to file input

I am stuck with a problem and I would like to get input from you guys.
I am coding a Neo4J application using py2neo. I want to read a file and use that file to create the nodes and relationships
The problem I have is that the file input using code below, gives the lines back as a string.
file = "../create_db"
dbFile=open(file,'r')
And what I need is, instead of getting it back as a string, to get it raw.
At the moment the problem is that I want:
graph_db.create(node({'Id':'1', 'Description':'Computer'}))
But I get:
graph_db.create("node({'Id':'1', 'Description':'Computer'})")
Is there a way to get file input raw? Maybe an library that gives it back raw?
Thanks in advance,
Jiar
It seem your input file contains code statements (or partial code statements).
You can execute the statements using the eval builtin function and pass the results of that to the graph_db.create function.
However, you should be aware this allows arbitrary code to be executed (i.e. the input file becomes part of the executing script) and should be treated as part of the code (i.e. don't use an untrusted input file).
You could also check the ast module. Although I don't know if this will work in your case (emphasis mine):
ast.literal_eval(node_or_string)
Safely evaluate an expression node or a Unicode or Latin-1 encoded string
containing a Python expression.
The string or node provided may only consist of the following Python literal structures: strings,
numbers, tuples, lists, dicts, booleans, and None.
This can be used for safely evaluating strings containing Python expressions
from untrusted sources without the need to parse the values oneself.
So maybe if you have some control on the file to only use the dict part…
Using eval can be dangerous. Check also this question and its answers.

Remove \xe2\x80\xa6 from string python

I have a lot of txt files, and I need to replace some text on them. Almost all of them has this non-ascii character (I thought it was "...", but … is not the same)
I've tried with replace() but I cannot make it, I need some help!! thanks in advance
If you use codecs.open() to open the files then you will get all strings as unicodes, which are much easier to handle.
Use unicode type strings. For example,
>>> print u'\xe2'.replace(u'\xe2','a')
a
the problem is that these characters are not valid string, they are unicode.
import re
re.sub(r'<string to repleace>','',text,re.U)
most other answers will work too

Python array conversion to a string

I am trying to write the contents of a re.findall to a file. I tried
output_file.write (findall(tags_pattern, searchtext))
but I got a type error. How do I convert this to a type that can be written to a file?
Thanks
The easiest way is to JSON-encode it. See the json module.
you have the str(res) and repr(res) function but you could also do ','.join(res)
re.findall returns a list of matches found in searchtext for tags_pattern. Those matches are just strings. The easiest way to convert a list of strings into a single string which can be written to a file is to call str.join on a string representing the separator you want to insert between the strings in the list. For example, you may call '\n'.join(findall(tags_pattern, searchtext)) if you want to store each match on its own line.
The pickle module is built to quickly store Python structures in a file. It is not nearly as portable as JSON or some other serialization format, but depending on your purposes, it may be just enough.
To use pickle:
import re, pickle
r = re.findall(pattern, text)
with open('results.pkl', 'wb') as resultsfile:
pickle.dump(r, resultsfile)
To recover the list, use pickle.load:
with open('results.pkl', 'rb') as resultsfile:
r2 = pickle.load(resultsfile)
I'd be wary of using this in production code, or where you need to transmit the re.findall results to a web client, but for quick testing and local storage, this is probably the easiest.

Python repr function problem

I'm dealing with some text parsing in Python and for that purpose, it's good for me to apply repr() function on each string I'm gonna parse, but after the parsing, I need to convert some parsed substring back to the previous representation, because I want to print them and I'm not able to do this. I thought that str() function should get the string back to the human more readable form. But when I apply str function on that substring, nothing's changed.
As I've said I need to print the string in human readable form, without printing escape sequences like \n, \t etc...
But when I apply repr() to a string and then I want to convert it back, I don't know how, because str() function didn't do it.
So my question is, how to convert the string back into human readable form?
Thanks for every reply.
str() has no effect on objects that are already strings. You need to use eval() to undo a repr() where possible. Try using ast.literal_eval() instead though.

Categories