Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load and format a CSV file
#11
(Oct-29-2019, 10:09 PM)snippsat Wrote:
(Oct-29-2019, 09:29 PM)fioranosnake Wrote: Hi, I there a straight forward way to write the pprint output to a text file?
with open('numb.csv') as f,open('out.csv', 'w') as f_out:
    #next(f) # If need to skip header
    data = [i.strip() for i in f]
    data = dict(enumerate(data, 1))
    for k,v in data.items():
        f_out.write(f'{k}: {v}\n')
Output:
1: 1231241231 2: 1235135135 3: 5457345345 4: 4577865568 5: 8654563848


Hi again, I'm totally grateful for you help. I think I may be trying to do things the hard way..... Essentially, I need to create a JSON file from a text file (eg);

1231241231
1235135135
5457345345
4577865568
8654563848


with the following format : (that I can pass as a payload to an API using requests library)

{"1": "1231241231",
"2": "1235135135",
"3": "5457345345",
"4": "4577865568",
"5": "8654563848"}

I guess I have been trying to confuse things by reading in the values, formatting them, writing them to a text file and then re-reading this in to the API app. No doubt it would be quicker (and easier) to do this in the same script.

Im already successfully running the API from a manually formatted 'source' file :

#Specify APIKey
APIKey = 'KEY'

with open("CodestoProcess.txt", "r") as f: 
     Codes =json.load(f)  

payload = {"Parameters":{"UserAccessKey": APIKey
                          },
           "Codes":Codes
          }
 
headers = {'content-type': 'application/json'}
  
response = requests.post(url, data=json.dumps(payload), headers=headers)
Id be so grateful if you could hekp creating a formatted JSON from the source file and make things far more slick.
Reply
#12
I don't fully understand objective but assuming that you have csv file named 'source-json.csv' with data on rows and want to have json one can simply do something along those lines:


import json
with open('source-json.csv', 'r') as f:
    d = {k: v.strip() for (k, v) in enumerate(f, start=1)}
    j = json.dumps(d)
One can write j to file or whatever:

Output:
{"1": "1231241231", "2": "1235135135", "3": "5457345345", "4": "4577865568", "5": "8654563848"}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Load a Folium map into a pdf-file Thats_Leet 0 649 Jan-01-2025, 08:13 PM
Last Post: Thats_Leet
  How to load all pages of the pdf file to the program alicenguyen 9 4,591 Jul-04-2022, 03:14 AM
Last Post: alicenguyen
  Reshape txt file into particular format using python shantanu97 0 2,047 Dec-10-2021, 11:44 AM
Last Post: shantanu97
  How can we transcode encoding file uml url format Anldra12 9 4,780 Jul-25-2021, 09:30 AM
Last Post: Anldra12
  How to design a save file format? philipbergwerf 5 5,402 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 3,241 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  Need help implmenting if/else or case statements for option to choose file format. samlee916 1 2,499 Jul-22-2020, 06:06 PM
Last Post: Larz60+
  copy/pasting in excel WHILE keep file format zarize 0 2,814 Jun-23-2020, 03:51 PM
Last Post: zarize
  Preserve xml file format tanffn 3 5,998 Jan-03-2020, 09:35 AM
Last Post: Larz60+
  Phyton code to load a comma separated csv file in to a dict and then in to a dB mrsenorchuck 2 3,384 Nov-29-2019, 10:59 AM
Last Post: mrsenorchuck

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020