I am trying to write data from json file to CSV file using python. My code is like this:
CSVFile1 = open('Group_A_participant_1_1.csv', 'a')
writeCSV1 = csv.writer(CSVFile1)
for file in data['annotations'][3]['instances']:
var = file['arguments'].get('argument1')
writeCSV1.writerow(var)
CSVFile.close()
My output is:
So my problem is that I can not see the whole word in one cell.
Thanks your helps inn advance!
I expect to get each word in one single cell.
Change
writeCSV1.writerow(var)
to
writeCSV1.writerow([var])
so you're writing an one-item list with your var instead of having the CSV module interpret var, a string, as separate characters.
For instance:
import csv
import sys
writeCSV1 = csv.writer(sys.stdout)
data = {
"annotations": [
{},
{},
{},
{
"instances": [
{"arguments": {"argument1": "foo"}},
{"arguments": {"argument1": "bar"}},
]
},
],
}
for file in data["annotations"][3]["instances"]:
var = file["arguments"].get("argument1")
writeCSV1.writerow([var])
prints out
foo
bar
whereas taking the brackets out from around [var] results in
f,o,o
b,a,r
as you described.
Click on the first cell of the column where you want the converted
names to appear (B2).
Type equal sign (=), followed by the text “Prof. “, followed by an
ampersand (&).
Select the cell containing the first name (A2).
Press the Return Key.
You will notice that the title “Prof.” is added before the first name in the list.
Related
I having some json format like
json= 5843080158430803{"name":"NAME", "age":"56",}
So, how i get {"name":"NAME", "age":"56",} Using regex/split (which one is bets method for it) in Python.
Thanks in Advance...
Split the first occurance of { into an array, and get the second element in the array.
We also have to add the { again because its removed by the split function
json = '5843080158430803{"name":"NAME", "age":"56",}'
json = '{' + json.split('{', 1)[1]
print(json)
Result: {"name":"NAME", "age":"56",}
perhaps you could split at at the first { and then replace the part prior to it.
I am assuming the json you have above is actually a string. Then you could do:
json_prefix = json.split("{")
json = json.replace(json_prefix, "")
Have a txt with contents :
{
hello : 1,two:three,four:five,six:seven,
}
how to remove the last , in the above string ?
while using them as dictionaries for further. it cant be parsed because of the last delimiter.
code :
import json
d2=json.load(open(test.txt))
i cant change the source code. coz i am extracting data from a json file(json.dump) and creating a new json. is there any way of doing that other than dump/changing the source code
This removes the last , in your string without the need of any further import's.
with open('test.txt', 'r') as f:
s = f.read()
s = s[::-1].replace(',', '', 1)[::-1]
the output of s is then:
{
hello : 1,two:three,four:five,six:seven
}
Simple replace should work fine.
broken_json = '''{
hello : 1,two:three,four:five,six:seven,
bye : 42,ick:poo,zoo:bar,}'''
j = broken_json.replace(',}', '}').replace(',\n}','\n}')
The result at this point is still not valid JSON, because the dictionary keys need to be quoted; but this is outside the scope of your question so I will not try to tackle that part.
string ='{hello : 1,two:three,four:five,six:seven,}'
length = len(string)
for i in range(length):
if(string[i] == ','):
string2 = string[0:i] + string[i + 1:length]
print (string2)
The output type would be a string. So, convert it to a dict later.
import re
string ='{hello : 1,two:three,four:five,six:seven,}'
match = re.search(r'(.*),[^,]*$', string)
print (match.group(1)+"}")
Try this #selcuk for an efficient one
I am new to python and i am working with some geojson files that contain a multiple objects each object represents a precinct. I need to print the coordinates for all of the precints how can i do this? i was trying this but it does not work:
import json
with open('districts and precinc data merged.json') as f:
data = json.load(f)
for i in json['features']:
print(i['geometry']['coordinates'])
this is an example of the json file:
{"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[552346.2856999999,380222.8998000007]]]]},"properties":{"OBJECTID":1,"STFID":"55001442500001","NAME":"0001"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[529754.7249999996,409135.9135999996],[529740.0305000003,408420.03810000047]]]},"properties":{"OBJECTID":2,"STFID":"55001537250001","NAME":"0001","COUSUBFP":"53725"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[508795.9363000002,441655.3672000002],[508813.49899999984,441181.034]]]},"properties":{"OBJECTID":6278,"STFID":"55141885750001","NAME":"0001","COUSUBFP":"88575"}}
]}
my desired output would be a for each line have the cordinates for each object like so:
[552346.2856999999,380222.8998000007]
[529754.7249999996,409135.9135999996],[529740.0305000003,408420.03810000047]
[508795.9363000002,441655.3672000002],[508813.49899999984,441181.034]
thank you for your help!
You have an error in your json. In first line you have 3 list open [[[ but closing 4 ]]]]. In your json file, replace [[[552346.2856999999,380222.8998000007]]]] with [[[552346.2856999999,380222.8998000007]]].
Then you can use nested loop,
x = {"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[552346.2856999999,380222.8998000007]]]},"properties":{"OBJECTID":1,"STFID":"55001442500001","NAME":"0001"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[529754.7249999996,409135.9135999996],[529740.0305000003,408420.03810000047]]]},"properties":{"OBJECTID":2,"STFID":"55001537250001","NAME":"0001","COUSUBFP":"53725"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[508795.9363000002,441655.3672000002],[508813.49899999984,441181.034]]]},"properties":{"OBJECTID":6278,"STFID":"55141885750001","NAME":"0001","COUSUBFP":"88575"}}
]}
for i in x["features"]:
for j in i["geometry"]["coordinates"][0]:
print(j, end=",") # replace `\n` with `,`
print("\b") # removes trailing ,
# output,
[552346.2856999999, 380222.8998000007]
[529754.7249999996, 409135.9135999996],[529740.0305000003, 408420.03810000047]
[508795.9363000002, 441655.3672000002],[508813.49899999984, 441181.034]
So i'm trying to insert a variable inside a text file and i've defined them as shown below:
for filename in os.listdir("/home/gyanender/Desktop/s1d_gyanender"):
if filename.endswith("_s1d_A.fits"):
df = pd.DataFrame.from_dict(SN_dic,orient='index')
df = df.mean()
out_name = s1d_header['OBJECT'] +'-' + s1d_header['DATE-OBS'] +'.ares'
mine_opt = '/home/gyanender/bin/ARES/mine.opt'
file_opt=open(mine_opt,'w')
file_opt.writelines(("specfits=filename","\n","fileout=out_name","\n","rejt=df"))
file_opt.close()
So in the end what i got is something like this, i got name of the variables in the text but what i want is the values of those variables in my text file as i've to give that file as an input to another code.
specfits='filename'
fileout='out_name'
rejt=df
As
filename = HARPS.2016-04-01T09:44:43.034_s1d_A.fits
out_name = Moon-2016-04-01T09:44:43.034.ares
rejt = 166.6 (As the values of df is 166.6)
So my ideal file should look like something like this:
specfits='HARPS.2016-04-01T09:44:43.034_s1d_A.fits'
fileout='Moon-2016-04-01T09:44:43.034.ares'
rejt= 166.6
So can someone tell me where i'm making the mistake?
You're doing
file_opt.writelines(("specfits=filename","\n","fileout=out_name","\n","rejt=df"))
Note that you're actually printing the string "specfits=filename" to a the file. There's nothing here to tell the code that that's not what you want it to print.
I think what you want is to substitute the variable name for the variable itself. This can be done using string formatting. Assuming python 3:
file_opt.writelines(("specfits={}".format(filename), "\n", "fileout={}".format(out_name), "\n", "rejt={}".format(df)))
Here, the {} in your string is replaced by whatever you give as an argument to the format() function.
If you want single-quotes around the values you can just put them in the string you're printing out, e.g. "specfits='{}'"
Don't try to invent the wheel, choose some storage method and use it.
This example uses json:
import json
data = { # create a dict with the data
'specfits': specfits,
'fileout': fileout,
'rejt': rejt,
}
with open(mine_opt, 'w') as file_opt:
json.dump(data, file_opt)
Then reading it will be very easy!
with open(mine_opt) as file_opt:
data = json.load(file_opt)
print(data['specfits']) # will print HARPS.2016-04-01T09:44:43.034_s1d_A.fits
I have such json file:
[{
"datafiles": ["data.data"]
}]
Description in .proto file:
message Dataset {
repeated string datafiles = 1;
}
When I create a Dataset (Dataset(datafiles=datafiles)) object datafiles sets up in strange manner:
datafiles: "d"\ndatafiles: "a"\ndatafiles: "t"\ndatafiles: "a"\ndatafiles: ."\ndatafiles: "d"\ndatafiles: "a"\ndatafiles: "t"\ndatafiles: "a"
How to set it in correct way:
datafiles: "data.data"
It looks like your string ("data.data") is being iterated and added one character at a time.
This suggests that you are probably passing in a string by itself:
"data.data"
when you should really be passing in an iterable containing strings:
[ "data.data" ]
Try printing the value of datafiles right before your call to create the Dataset:
print(repr(datafiles))
... whatever ... Dataset(datafiles=datafiles)