Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable list
#1
Hi!

I'm brand new to python FYI.

I'm trying to create a text list with a changing variable on each line e.g.
Output:
example001test001 example002test002 example003test003 ...
So far, from searching online I've got as far as this:
values = "example{}test{}"
id = ["%.3d" % i for i in range(1, 100)]
result = values.format(id, id)
outputfile = "Z:\RESULT.csv"
import csv
with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in links:
        writer.writerow([result]) 
The problem is when I run this it isn't exporting the variables one per line, it's showing the whole list on each line instead. Any ideas?

Many thanks!
Reply
#2
(Aug-18-2018, 09:02 PM)Ivan86 Wrote:
    for val in links:
        writer.writerow([result]) 
Why have a loop, if you don't use what you're looping over?
>>> spam = "eggs"
>>> for num in range(5):
...   print(spam) # why am i looping?
...
eggs
eggs
eggs
eggs
eggs
Reply
#3
It's hard to say, as it is not clear how you have defined some of the variables in your code. For example, you loop over links, but links is not defined anywhere in your code. Further, you are looping over links with the loop variable val, but you are not writing val. You are writing result, which is defined once outside the loop, and never changes in the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
oops, I mistyped it. It's actually

values = "example{}test{}"
id = ["%.3d" % i for i in range(1, 100)]
result = values.format(id, id)
outputfile = "Z:\RESULT.csv"
import csv
with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in result:
        writer.writerow([result]) 
I have no idea what you mean by loop sadly. As I said, I'm brand new to python, sorry.
Reply
#5
(Aug-18-2018, 09:17 PM)Ivan86 Wrote:
    for val in result:
        writer.writerow([result]) 

That's still exactly the same situation, though. You've got a few things in a bag, and then you say "for each thing in this bag, look at the bag". The file has everything listed multiple times, because you're writing everything to the file, over and over again.
Reply
#6
The 'for' statement creates a loop. Nilamo copied the two lines of your code that are the loop. 'for bar in foo' goes through a value in foo, assigns it to the variable bar, and then processes whatever is indented under the for statement. It repeats that for every value in foo.

So in your loop, you are assigning values to val, but then never using those values. And note that result is a string. So each time through the loop val is going to be a different character in that string. That is, when you create result on line, you're not creating a list of all the things you want to output, you are creating one thing, which is not what you want to output.

You need to put the values text and the format statement inside the list comprehension that you use for id. And you shouldn't call it id. That is a built-in function in Python. Assigning a value to it removes access to the built-in and can mess up later code that uses it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Ok thanks for the responses.

I understand where it's going wrong I think but I'm not sure how to fix it to do what I'm after. I've renamed some of the variables now.

Getting rid of the example, this is my actual code I'm trying to produce:
template = "ext{}hvac{}class"
digits = ["%.3d" % i for i in range(1, 179)]
formatted = template.format(digits, digits)
csvfile = "Z:\RESULT.csv"
I'm trying to get the result
Output:
ext001hvac001class ext002hvac002class ext003hvac003class ...
what do i need to do to output the file correctly? Is it the "formatted" I don't need? I don't know how to link the digits list to the template output otherwise. I either seem to get one character per line or the whole string per line repeated or a line of special characters repeated over and over.
Reply
#8
Look at the format string syntax. That will show you how to get the number format you have in line 2 of your latest post into the format string on line 3. Then put the format statement into the list comprehension on line 2.

Then make sure your for loop is looping over digits.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Can't see where it explains that.

I'm just going to give up, I could have just typed it all out in less time than it's taken so far.

Just did it in excel in 5 mins instead.
Reply
#10
(Aug-19-2018, 07:21 AM)Ivan86 Wrote: Can't see where it explains that.
It more general how string formatting work.
(Aug-19-2018, 07:21 AM)Ivan86 Wrote: I'm just going to give up, I could have just typed it all out in less time than it's taken so far.
Just did it in excel in 5 mins instead.
Isn't the point that you try to learn Python Wink

Look like this with new string formatting f-string.
with open('result.csv', 'w') as f_out:
    for i in range(1, 6):
        f_out.write(f'example{i:0>3d}test{i:0>3d}\n')
Output:
example001test001 example002test002 example003test003 example004test004 example005test005
Here a fix to your code,if you wonder what was wrong.
import csv

values = "example{}test{}"
lst = ["%.3d" % i for i in range(1, 6)]
result = values.format(id, id)
outputfile = "result.csv"
with open(outputfile, "w", newline='\n') as output:
    writer = csv.writer(output)
    for val in lst:
        line = values.format(val, val)
        writer.writerow([line])
Output:
example001test001 example002test002 example003test003 example004test004 example005test005
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string using variable found in a list japo85 2 1,237 Jul-11-2022, 08:52 AM
Last Post: japo85
  An IF statement with a List variable dedesssse 3 7,948 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Create variable and list dynamically quest_ 12 4,287 Jan-26-2021, 07:14 PM
Last Post: quest_
Question Matching variable to a list index Gilush 17 5,701 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  Print variable values from a list of variables xnightwingx 3 2,571 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Multiplication between a list and a variable doug2019 2 2,122 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Sub: Python-3: Better Avoid Naming A Variable As list ? adt 9 3,937 Aug-29-2019, 08:15 AM
Last Post: adt
  Score each word from list variable pythonias2019 6 3,297 Jun-13-2019, 05:44 PM
Last Post: gontajones
  How to store the value from variable into list & run a statement then put in variable searching1 1 2,413 May-29-2019, 06:36 AM
Last Post: heiner55
  copy list into variable poroton 1 2,573 Aug-10-2018, 07:19 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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