我理解课程FileWriter和File Reader.
我想知道这个班级RandomAccessFile与FileWriter和FileReader.相比.
我有两个具有相同行数的文件,每行包含数值列.
文件示例 A
1 2 3 4
2 3 4 5
Run Code Online (Sandbox Code Playgroud)
文件示例 B
7 8 9 0
6 7 8 9
Run Code Online (Sandbox Code Playgroud)
我想从这两个文件中求和相应行的值,并将结果写入输出文件.
预期产量:
8 10 12 4
8 10 12 14
Run Code Online (Sandbox Code Playgroud) 我试图读取一个名为的文件的内容,pp.txt并在命令行上显示它的内容.我的代码是:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *f;
float x;
f=fopen("pp.txt", "r");
if((f = fopen("pp.txt", "r")) == NULL)
{
fprintf(stderr, "Sorry! Can't read %s\n", "TEST1.txt");
}
else
{
printf("File opened successfully!\n");
}
fscanf(f, " %f", &x);
if (fscanf(f, " %f ", &x) != 1) {
fprintf(stderr, "File read failed\n");
return EXIT_FAILURE;
}
else
{
printf("The contents of file are: %f \n", x);
}
fclose(f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译后我得到了File opened successfully!File read failed.我的内容pp.txt是34.5.谁能告诉我哪里出错了?
我想读取XML文件并保存InnerText-Strings,但它会因SystemNullReference错误而崩溃.我认为我读它的方式是错误的,但我不确定.
XML的文件:
<?xml version="1.0" encoding="Windows-1252" standalone="no"?>
<schema>
<Hintergrund>#FFFFFFFF</Hintergrund>
<Zahlen>#FFFFFFFF</Zahlen>
<Text>#FFFFFFFF</Text>
<Zeichenketten>#FFFFFFFF</Zeichenketten>
<Tags>#FF000000</Tags>
<Direktiven>#FF000000</Direktiven>
<Ausdruecke>#FF000000</Ausdruecke>
<Initialisierer>#FF000000</Initialisierer>
<Modifizierer>#FF000000</Modifizierer>
<Booleanische>#FF000000</Booleanische>
<Operator>#FF000000</Operator>
<Kommentare>#FF000000</Kommentare>
</schema>
Run Code Online (Sandbox Code Playgroud)
读:
foreach (XmlNode node in doc.SelectNodes("/schema/*"))
{
switch (node.Name)
{
case "Hintergrund":
colorBG = (Color)ColorConverter.ConvertFromString(node["Hintergrund"].InnerText);
break;
case "Zahlen":
colorNumbers = (Color)ColorConverter.ConvertFromString(node["Zahlen"].InnerText);
break;
case "Text":
colorText = (Color)ColorConverter.ConvertFromString(node["Text"].InnerText);
break;
case "Zeichenketten":
colorStrings = (Color)ColorConverter.ConvertFromString(node["Zeichenketten"].InnerText);
break;
case "Tags":
colorTags = (Color)ColorConverter.ConvertFromString(node["Tags"].InnerText);
break;
case "Direktiven":
colorDirectives = (Color)ColorConverter.ConvertFromString(node["Direktiven"].InnerText);
break;
case "Ausdruecke":
colorStatements = (Color)ColorConverter.ConvertFromString(node["Ausdruecke"].InnerText);
break;
case "Initialisierer":
colorInitial = (Color)ColorConverter.ConvertFromString(node["Initialisierer"].InnerText);
break;
case "Modifizierer": …Run Code Online (Sandbox Code Playgroud) 我试图.jpg使用requestspython中的模块从URL 下载文件.这是我试过的.没有错误.但是我无法打开输出文件.
>>> import requests
>>> l = requests.get("http://www.mosta2bal.com/vb/imgcache/2/9086screen.jpg")
>>> l
<Response [200]>
>>> l.text
u'\ufffd\ufffd\ufffd\ufffd\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\ufffd\ufffd\x12EExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x......long text
>>> l.encoding
>>> import codecs
>>> f = codecs.open('out.jpg', mode="w", encoding="utf-8")
>>> f.write(l.text)
Run Code Online (Sandbox Code Playgroud) 我想要一个功能,当我的应用程序崩溃时,已写入的日志保存在日志文件中.但是使用fopen()/fwrite()from cstdio库似乎不会这样做.因此,当我的应用程序崩溃时,日志文件为0KB.我想知道是否有任何文件处理库可以做到这一点?例如Boost?
这是我的代码:
import random
ch1=input("Please enter the name of your first character ")
strch1=(((random.randint(1,12)//(random.randint(1,4))))+10)
sklch1=(((random.randint(1,12)//(random.randint(1,4))))+10)
print("The strength value of "+ch1+" is:")
print (strch1)
print("and the skill value of "+ch1+" is:")
print (sklch1)
ch2=input("Please enter the name of your second character ")
strch2=(((random.randint(1,12)//(random.randint(1,4))))+10)
sklch2=(((random.randint(1,12)//(random.randint(1,4))))+10)
print("The strength value of "+ch2+" is:")
print (strch2)
print("and the skill value of "+ch2+" is:")
print (sklch2)
myFile = open("CharacterSkillAttributes.txt", "wt")
myFile.write("The attributes of "+ch1+" are: /n")
myFile.write("Strength: "+strch1+"/n")
myFile.write("Skill: "+sklch1+"/n")
myFile.write("The attributes of "+ch2+" are: /n")
myFile.write("Strength: …Run Code Online (Sandbox Code Playgroud) 如何json object在sdcard中保存文件?
我目前正在使用以下语句将文件的每一行写入列表
try:
list = []
with open(file, 'r') as f:
for l in f.readlines():
list.append(l)
except:
...
Run Code Online (Sandbox Code Playgroud)
虽然它工作得很好,我想知道是否有更多的pythonic方式来做到这一点?
编辑: 使用建议的更新
try:
my_list = []
with open(file, 'r') as f:
my_list = f.readlines()
except IOError:
...
Run Code Online (Sandbox Code Playgroud) Traceback (most recent call last):
File "wdd.py", line 164, in <module>
file.write("temperature is ", temperature, "wet is ", humidity, "%\n")
TypeError: function takes exactly 1 argument (5 given)
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 26 14:24:43 2014
@author: pi
"""
import smtplib
import RPi.GPIO as gpio
import time
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
time.sleep(1)
data=[]
def delay(i): #20*i usdelay
a=0
for j in range(i):
a+1
j=0
#start work
gpio.setup(7,gpio.OUT)
#gpio.output(12,gpio.HIGH)
#delay(10)
gpio.output(7,gpio.LOW)
time.sleep(0.02)
gpio.output(7,gpio.HIGH)
i=1
i=1
#wait to …Run Code Online (Sandbox Code Playgroud)