Variable list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Variable list (/thread-12298.html) |
Variable list - Ivan86 - Aug-18-2018 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. 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! RE: Variable list - nilamo - Aug-18-2018 (Aug-18-2018, 09:02 PM)Ivan86 Wrote:Why have a loop, if you don't use what you're looping over?for val in links: writer.writerow([result]) >>> spam = "eggs" >>> for num in range(5): ... print(spam) # why am i looping? ... eggs eggs eggs eggs eggs RE: Variable list - ichabod801 - Aug-18-2018 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. RE: Variable list - Ivan86 - Aug-18-2018 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. RE: Variable list - nilamo - Aug-18-2018 (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. RE: Variable list - ichabod801 - Aug-18-2018 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. RE: Variable list - Ivan86 - Aug-18-2018 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 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.
RE: Variable list - ichabod801 - Aug-18-2018 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. RE: Variable list - Ivan86 - Aug-19-2018 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. RE: Variable list - snippsat - Aug-19-2018 (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.Isn't the point that you try to learn Python 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') 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])
|