Python not reading\writing in 2nd file in for loop [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Im trying to read two files and print them in another two separate files after reading and making some updates. My input file names are big-1.csv and big-2.csv. Therefore Im trying to read these two files using a for loop. For the output im trying to print them with names fix-1.csv and fix-2.csv but it seems my for loop is not running for the second time and only running once, while only reading and writing the first big-1.csv file to fix-1.csv file.
My code is :
import csv
from csv import DictWriter
for i in range(1,2):
print(i) #just a flag to check
with open("big-" + str(i) + ".csv") as people_file:
next(people_file)
corrected_people = []
for person_line in people_file:
chomped_person_line = person_line.rstrip()
person_tokens = chomped_person_line.split(",")
# check that each field has the expected type
try:
corrected_person = {
"id": person_tokens[0],
"first_name":person_tokens[1],
"last_name": "".join(person_tokens[2:-3]),
"email":person_tokens[-3],
"gender":person_tokens[-2],
"ip_address":person_tokens[-1]
}
if not corrected_person["ip_address"].startswith(
"") and corrected_person["ip_address"] !="n/a":
raise ValueError
corrected_people.append(corrected_person)
except (IndexError, ValueError):
# print the ignored lines, so manual correction can be performed later.
print("Could not parse line: " + chomped_person_line)
with open("fix-" + str(i) + ".csv", "w") as corrected_people_file:
writer = DictWriter(
corrected_people_file,
fieldnames=[
"id","first_name","last_name","email","gender","ip_address"
],delimiter=',')
writer.writeheader()
writer.writerows(corrected_people)
Th output im getting is :
j:\Programs\Python>python "for loop testing.py"
1
j:\Programs\Python>
And fix-1.csv file. The update part is working fine. The only problem I'm facing is that the for loop is running once. Please note, no indentation error is coming. Please help.

range(1,2) only contains one value (the number 1).
Perhaps you meant
for i in (1,2): # values are 1 and 2
or
for i in range(2): # values are 0 and 1
or
for i in range(1,3): # values are 1 and 2

Related

how to store a dictionary as a json file and edit it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a function that creates a dictionary based on the user's input:
def store_data(user_inp):
list_of_letters = list(user_inp)
list_of_colons = []
nested_dict = {}
for letter in list_of_letters:
if letter == ':':
list_of_colons.append(letter)
if len(list_of_colons) == 2:
str1 = ''.join(list_of_letters)
list2 = str1.split(':')
main_key = list2[0]
nested_key = list2[1]
value = list2[2]
if main_key not in storage:
storage[main_key] = nested_dict
nested_dict[nested_key] = value
print(storage, '\n', 'successfully saved!')
elif main_key in storage:
if nested_key in storage[main_key]:
print('this item is already saved: \n', storage)
else:
storage[main_key][nested_key] = value
print(storage, '\n', 'successfully saved!')
jf = json.dumps(storage)
with open('myStorage.json', 'w') as f:
f.write(jf)
f.close()
What i'm trying to do is to store the final dictionary somewhere permanent.
I tried this at the end of my function but it doesn't seem to work:
jf = json.dumps(storage)
with open('myStorage.json', 'w') as f:
f.write(jf)
f.close()
How can I store the final dictionary so it's permanent but still editable?
You can save it to a .json file as you did. After that, you can still edit the variable that you pasted. So you could create a thread that auto-saves every 10 minutes or so by invoking
jf = json.dumps(storage)
with open('myStorage.json', 'w') as f:
f.write(jf)
PS: You don't need to care about f.close() if you are using with open(...) :)
If you can't tell what is happening where I highly suggest printing the current state of storage before entering a new if clause
I'm sorry but I am unable to debug your code because there are to many variables undefined...

How to sort an entire file on a specifc data/column? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
[data.txt]
CODE\tUSERNAME\tSPENT\tCOLUM1\tCOLUM2
I want to sort the file [data.txt], using "SPENT". How can i do this?
Yes, of course it is possible. For example:
# read file into array of lines
lines = open("data.txt").readlines()
# sort those lines using a lambda
lines.sort(key = lambda line : line.split("\t")[2])
The lambda extrudes the SPENT column from the row to be used as sorting-key.
def subMenu_5():
# read file into array of lines
lines = open("database").readlines()
# sort those lines using a lambda
lines.sort(key = lambda line : line.split("\t")[3])
clientList = []
dataQuantify = 0
database = open('database','r')
i = 1
while (i == 1):
if (database.readline() == ''):
i = 0
else:
dataQuantify = dataQuantify + 1
database.close()
sortList = open("sortList","w")
for i in range (3):
sortList.write(lines[i])
sortList.close()
print "[Código] [Nome] [Quant. Prod. Comprados] [Valor Gasto] [Descontos] \n"
sortList = open('sortList','r')
i = 0
while (i < dataQuantify):
clientList.append(sortList.readline())
print clientList[i]
i = i + 1
database.close()
raw_input("Precione 'ENTER' para voltar ao menu principal... ")
return
This work! Very Thx!

Reading data from txt file only reads last line [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to print data from my text file into python
text_file = open ("Class1.txt", "r")
data = text_file.read().splitlines()
for li in data:
namelist = li.split(":")[0]
scorelist = li.split(":")[1]
print (namelist)
print (scorelist)
text_file.close()
My text file has:
Jim:13524
Harry:3
Jarrod:10
Jacob:0
Harold:5
Charlie:3
Jj:0
It only shows the last entry
Shell:
Would you like to view class 1, 2 or 3? 1
Jj
0
The problem is that you are over-writing the value of namelist and scorelist with each pass through the loop. You need to add each item to a list. Adding a sequential list of items to a list is usually done with list.append() or a list comprehension. Read the documentation, or do some tutorials?
To actually create list, you can do this:
namelist, scorelist = [],[]
for li in data:
namelist.append(li.split(":")[0])
scorelist.append(li.split(":")[1])
Alternately, this might be a better overall approach:
with open("Class1.txt", "r") as text_file:
names_scores = [(e[0],e[1]) for e in [li.split(":") for li in text_file]
for name,score in name_scores:
print(name,score)
This assumes you really just want to extract the names and scores and print them, not do anything else. How you handle and store the data depends a lot on what you are doing with it once you extract from the file.

Overwrite variable to file in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I defined a function that generates some names and I run from loop:
output = open('/tmp/NameGen-output.txt', 'w')
while True:
var = NameGen(name)
.
.
.
if sth:
output.write(var)
elif other:
output.write(var)
break
else:
break
output.close()
update:
first iteration ,content of NameGen-output.txt:
a
b
c
second iteration:
a
b
c
d
e
and etc
So if I overwrite it the second iteration would be just:
d
e
What I am going to ask is:
As you see var equals NameGen() and for each iteration content of var is written to NameGen-output.txt but I want to overwrite output of for each iteration of NameGen() to NameGen-output.txt no appending to it.
Could you possibly help me?
Thank you
You can truncate the existing file without opening and closing it, and flush to ensure that it is written to:
output = open('/tmp/NameGen-output.txt', 'w')
while True:
var = NameGen()
.
.
.
if not sth and not other:
break
else:
output.flush()
output.seek(0)
output.truncate()
output.write(var)
output.flush()
output.write(var)
if other:
break
output.close()
You could move the file opening (note: using with context manager is preferred) inside the loop:
while True:
var = NameGen()
...
with open('/tmp/NameGen-output.txt', 'w') as output:
output.write(var)
...
...

Python TypeError: unsupported operand types for +: 'tag' and 'str' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using numbers from a specific column of an excel document(csv) to populate a URL, from which cURL extracts XML and places into a new column of the outfile (also an excel document). This process is repeated for each id in the column. I cannot figure out why I am getting this error, as the output is in fact a string, and I do not see why you would not be able to concatenate it with a 'tab' buffer. I also thought I should ask this since I did not see any other questions relating this error to tags, maybe someone else can benefit from it as well. Anyway here is some code let me know if more information is needed, I have marked where the error happens (near the bottom):
outFile = open(tempFileName, 'w')
outFile.write('\t'.join(fancyHeaders) + '\n')
outFile.write('\t'.join(order) + '\n')
lastFN = False
for line in data:
if lastFN!=line['AppStatus'] and lastFN:
outFile.write('\n')
for column in order:
outFile.write(line[column] + '\t') #Error occurs here
lastFN = line['AppStatus']
outFile.write('\n')
xlApp.Workbooks.OpenText(tempFileName)
xlApp.Range("A1:Z9999").HorizontalAlignment = -4131
xlApp.Range("A1:Z9999").VerticalAlignment = -4160
xlApp.Range("A1:Z9999").WrapText = True
xlApp.Cells.RowHeight=12.75
xlApp.DisplayAlerts=False
xlApp.ActiveWorkbook.SaveAs(outFileName)
xlApp.Quit()
xlApp.Visible = 0 # see note 2
del xlApp
if the line[column] is not string, you cannot concatenate it, then try to change:
str(line[column] + '\t')
into:
str(line[column]) + '\t'
Couldn't you just write it that way ?
outFile.write(str(line[column]) + '\t')

Categories